【发布时间】:2014-02-26 00:13:50
【问题描述】:
我正在为一个班级制作一个 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>
主页.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);
}
}
登录.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);
}
}
【问题讨论】:
标签: android button android-activity