我正在为一个班级制作一个Android应用程序,到目前为止我已经开始了,但是一旦应用程序启动,你点击屏幕,它就会在按钮不可见时做任何事情,或者当按钮可见时,它会崩溃应用程序。它应该转移到一个新的活动。 (登录)
错误就是这个
02-25 18:02:54.250: E/AndroidRuntime(25252): FATAL EXCEPTION: main
02-25 18:02:54.250: E/AndroidRuntime(25252): java.lang.IllegalStateException: Could not find a method onClick (View v)(View) in the activity class jr.crfbla.etn.main for onClick handler on view class android.widget.Button with id 'button1'
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.view.View$1.onClick(View.java:3839)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.view.View.performClick(View.java:4489)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.view.View$PerformClick.run(View.java:18803)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.os.Handler.handleCallback(Handler.java:730)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.os.Looper.loop(Looper.java:137)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.app.ActivityThread.main(ActivityThread.java:5455)
02-25 18:02:54.250: E/AndroidRuntime(25252): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 18:02:54.250: E/AndroidRuntime(25252): at java.lang.reflect.Method.invoke(Method.java:525)
02-25 18:02:54.250: E/AndroidRuntime(25252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-25 18:02:54.250: E/AndroidRuntime(25252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-25 18:02:54.250: E/AndroidRuntime(25252): at dalvik.system.NativeStart.main(Native Method)
02-25 18:02:54.250: E/AndroidRuntime(25252): Caused by: java.lang.NoSuchMethodException: onClick (View v) [class android.view.View]
02-25 18:02:54.250: E/AndroidRuntime(25252): at java.lang.Class.getConstructorOrMethod(Class.java:423)
02-25 18:02:54.250: E/AndroidRuntime(25252): at java.lang.Class.getMethod(Class.java:787)
02-25 18:02:54.250: E/AndroidRuntime(25252): at android.view.View$1.onClick(View.java:3832)
对纳什维尔清单的探索
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jr.crfbla.etn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/NoActionBar" >
<activity
android:name="main"
android:label="@string/app_name"
android:theme="@style/NoActionBar"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="login"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/NoActionBar"
android:parentActivityName="main" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="main" />
</activity>
</application>
</manifest>
Home.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_gravity="bottom"
android:background="@drawable/home_page"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape"
tools:context=".main" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusableInTouchMode="true"
android:longClickable="false"
android:onClick="onClick (View v)"
android:paddingLeft="96dp"
android:text="@+string/Blank"
android:visibility="invisible" />
</RelativeLayout>
main.java
package jr.crfbla.etn;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class main extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
/** Called when the activity is first created/ */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
/** Called when the user clicks the Start button */
// Do something in response to button
public void onClick (View button1) {
Intent loginintent = new Intent(main.this, login.class);
main.this.startActivity(loginintent);
}
}
login.xml
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/log_in_page"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape"
tools:context="login" >
</RelativeLayout>
login.java
package jr.crfbla.etn;
import android.app.Activity;
import android.os.Bundle;
public class login extends Activity {
/** Called when the activity is first created/ */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
答案 0 :(得分:1)
替换
android:onClick="onClick (View v)"
带
android:onClick="onClick"
答案 1 :(得分:0)
您的Activity
类应该实现View.OnClickListener
接口,然后覆盖相应的onClick
方法。正如其他人所指出的那样,布局XML中的onClick
声明也是错误的,它必须读取“onClick”以匹配被覆盖的onClick
方法。
答案 2 :(得分:0)
请尝试以下说明:
//inside your layout file
<Button
android:id="@+id/mButton"
android:text="Start"
android:background="#ffffff" />
现在,在你的活动中试试这个:
public class MyActivity extends Activity implements View.OnClickListener{
Button mButton;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main.xml);
mButton = (Button)findViewById(R.id.mButton);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.mButton:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
break;
default:
//nothing happened
}
}
}
这种处理点击事件的方式确保您可以编写最小行,因为您只需要设置OnClickListener - 每个按钮一行,并使用switch语句检查单击了哪个按钮。如果你有更多的活动,你可以定义一个intent然后在每个按钮内实例化它们,具体取决于点击了哪个按钮
祝你好运!答案 3 :(得分:0)
您也不需要使用很长的方式来调用活动,尤其是在活动中调用它时。只需使用
startActivity(loginintent);