【问题标题】:Android app is not RunningAndroid 应用程序未运行
【发布时间】: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


【解决方案1】:

去吧

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>

【讨论】:

  • 在此处评论您的主要活动的第 19 行!我会更新我的回复并将最终解决方案发布给您!
  • Intent i = new Intent(MainActivity.this, About.class);开始活动(一); }
  • 您需要的任何其他内容。非常需要您的帮助,因为我坚持了 4 天。
  • 用按钮和about类附加布局!
  • onClick中的代码只有在点击按钮时才会执行,但是在activity启动时app已经崩溃了。
【解决方案2】:

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

@覆盖 public void onClick(View v) {

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

}

【讨论】:

  • 在这种情况下,请检查您的 layout.xml 文件中的 MainActivity 并确保您的按钮 ID 已定义,并且拼写/字母大小写与您的 findViewById 中定义的相同
【解决方案3】:

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

当您将 ClickListener 分配给按钮时,您能否检查按钮是否不为空?

在分配按钮或添加后放置断点

Log.d(--YOURTAG--, --BUTTONNAME-- == null ? "Button null":"Button not null");

分配按钮后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多