Android应用未运行

时间:2014-01-11 21:15:59

标签: java android xml eclipse android-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>

任何帮助? 我希望我的应用程序运行并显示主页面,当我点击关于按钮时,必须启动about活动 这是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

在添加关于按钮和类之前更多的事情。应用程序运行良好。

3 个答案:

答案 0 :(得分:0)

使用此

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.nomzapp.About;

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(MainActivity.this, About.class);
            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" />
     <activity android:name="com.nomzapp.About"
        android:label="@string/app_name"/>

</application>

答案 1 :(得分:0)

onclick事件处理程序应如下所示

@覆盖  public void onClick(查看v){

        Intent i = new Intent(MainActivity.this, ABOUT.class);
        startActivity(i);

}

答案 2 :(得分:0)

您加载的布局似乎不包含ID为R.id.aboutbu或R.id.exitbu的按钮。

您可以在将ClickListeners分配给它们时检查按钮是否为空吗?

在分配按钮或添加

后,只需放置断点即可
Log.d(--YOURTAG--, --BUTTONNAME-- == null ? "Button null":"Button not null");

分配按钮后。