Я новичок в программировании на Android, и я попытался выяснить, как исправить эту ошибку, однако я до сих пор не могу понять, что я сделал неправильно. Я просто следую инструкциям по youtube и решил создать свою собственную программу, чтобы практиковать. Я еще не закончил с программой, но в основном, я пытаюсь связать кнопку из моего .main xml (aka menu class) на страницу triangle_guide.xml, но по какой-то причине она не работает. Пожалуйста помоги. большое спасибо.Приложение неожиданно остановилось Errór
LogCat
09-14 04:44:16.444: E/AndroidRuntime(277): FATAL EXCEPTION: main
09-14 04:44:16.444: E/AndroidRuntime(277): java.lang.IllegalStateException: Could not find a method btnGuide(View) in the activity class com.example.trianglegame.MainActivity for onClick handler on view class android.widget.Button with id 'btnGuide'
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$1.onClick(View.java:2059)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View.performClick(View.java:2408)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$PerformClick.run(View.java:8816)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Handler.handleCallback(Handler.java:587)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
09-14 04:44:16.444: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-14 04:44:16.444: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-14 04:44:16.444: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
09-14 04:44:16.444: E/AndroidRuntime(277): Caused by: java.lang.NoSuchMethodException: btnGuide
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.Class.getMethod(Class.java:985)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$1.onClick(View.java:2052)
09-14 04:44:16.444: E/AndroidRuntime(277): ... 11 more
MainActivity Java Code
package com.example.trianglegame;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Кнопка меню Класс
package com.example.trianglegame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MenuBtn extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGuide = (Button) findViewById(R.id.btnGuide);
btnGuide.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), TriangleGuide.class);
startActivityForResult(intent, 0);
}
});
}
}
Руководство Класс
package com.example.trianglegame;
import android.app.Activity;
import android.os.Bundle;
public class TriangleGuide extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.triangle_guide);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trianglegame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TriangleGuide"></activity>
</application>
</manifest>
.main XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.trianglegame.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/Welcome"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:text="@string/select_option" />
<Button
android:id="@+id/btnGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="34dp"
android:layout_marginTop="113dp"
android:text="@string/guide_btn"
android:onClick="btnGuide" />
<Button
android:id="@+id/btnGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/btnGuide"
android:layout_marginLeft="34dp"
android:text="@string/game_btn" />
</RelativeLayout>
triangle_guide.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/t_tri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/e_tri"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/e_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/e_tri"
android:src="@drawable/e_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/e_tri" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/i_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/i_tri"
android:src="@drawable/i_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/i_tri" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/s_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/s_tri"
android:src="@drawable/s_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/s_tri" />
<TextView>
</TextView>
</LinearLayout>
</ScrollView>
Спасибо вам так много! – Chidori