【发布时间】:2014-01-11 21:15:59
【问题描述】:
大家好,我有问题。 我正在尝试构建一个应用程序,我加入了主活动中的一个按钮,并尝试连接另一个活动,该活动只需单击此按钮即可打开。我这里用的intent是java代码
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button exitbu;
Button aboutbu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aboutbu = (Button) findViewById(R.id.aboutbu);
aboutbu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.nomzapp.ABOUT");
startActivity(i);
}
});
exitbu= (Button) findViewById(R.id.exitbu);
exitbu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.exit(0);
}
});
@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;
}
}
但是当我在模拟器或移动设备中运行这个应用程序时。该应用程序在启动画面后停止。 (出现强制关闭消息框) 这是宣言
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nomzapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.nomzapp.Splash"
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="com.nomzapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.nomzapp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.nomzapp.About"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.nomzapp.ABOUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
有什么帮助吗?? 我希望我的应用程序运行并出现主页,当我单击关于按钮时,关于活动必须启动 这里是 LogCat
01-12 02:27:49.972: D/dalvikvm(540): GC_EXTERNAL_ALLOC freed 43K, 53% free 2552K/5379K, external 1625K/2137K, paused 243ms
01-12 02:27:51.088: D/dalvikvm(540): GC_EXTERNAL_ALLOC freed 1K, 53% free 2551K/5379K, external 2580K/3222K, paused 229ms
01-12 02:27:56.507: D/dalvikvm(540): GC_EXTERNAL_ALLOC freed 5K, 53% free 2569K/5379K, external 2699K/3654K, paused 70ms
01-12 02:27:56.737: D/dalvikvm(540): GC_EXTERNAL_ALLOC freed <1K, 53% free 2569K/5379K, external 3824K/4776K, paused 61ms
01-12 02:27:56.948: D/AndroidRuntime(540): Shutting down VM
01-12 02:27:56.948: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-12 02:27:56.958: E/AndroidRuntime(540): FATAL EXCEPTION: main
01-12 02:27:56.958: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nomzapp/com.nomzapp.MainActivity}: java.lang.NullPointerException
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:123)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-12 02:27:56.958: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 02:27:56.958: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke (Method.java:507)
01-12 02:27:56.958: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 02:27:56.958: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 02:27:56.958: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method)
01-12 02:27:56.958: E/AndroidRuntime(540): Caused by: java.lang.NullPointerException
01-12 02:27:56.958: E/AndroidRuntime(540): at com.nomzapp.MainActivity.onCreate(MainActivity.java:19)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-12 02:27:56.958: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-12 02:27:56.958: E/AndroidRuntime(540): ... 11 more
01-12 02:27:59.248: I/Process(540): Sending signal. PID: 540 SIG: 9
在添加按钮和类之前还有更多内容。应用运行良好。
【问题讨论】:
-
我发布了按钮正常运行的解决方案。您能否将崩溃的 logcat 附加到您的问题中?
-
你不应该使用
System.exit(0);。相反,尝试类似finish() -
iGio90 我附上logcat
-
gian1200 运行良好。
-
请发布您的布局 xml 文件。
标签: java android xml eclipse android-intent