【问题标题】:Unfortunately, (my app name) has stopped [duplicate]不幸的是,(我的应用程序名称)已停止[重复]
【发布时间】:2015-04-14 22:24:08
【问题描述】:

我是 android 应用程序开发的新手,我正在使用 eclipse。我刚刚创建了 2 个活动,并在主要活动中添加了一个按钮,假设它只是打开空白的第二个活动。当我尝试在手机上运行该应用程序时,它会一直停止。

这是eclipse中的logcat:

04-14 21:16:07.373: E/Trace(27640): error opening trace file: No such file 

or directory (2)

04-14 21:16:07.603: E/AndroidRuntime(27640): FATAL EXCEPTION: main
04-14 21:16:07.603: E/AndroidRuntime(27640): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.inveslator/com.example.inveslator.MainActivity}: java.lang.NullPointerException
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.os.Looper.loop(Looper.java:213)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread.main(ActivityThread.java:4787)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at dalvik.system.NativeStart.main(Native Method)
04-14 21:16:07.603: E/AndroidRuntime(27640): Caused by: java.lang.NullPointerException
04-14 21:16:07.603: E/AndroidRuntime(27640):    at com.example.inveslator.MainActivity.onCreate(MainActivity.java:21)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.Activity.performCreate(Activity.java:5008)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-14 21:16:07.603: E/AndroidRuntime(27640):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
04-14 21:16:07.603: E/AndroidRuntime(27640):    ... 11 more

下面是androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.inveslator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".Second">
        </activity>

    </application>

</manifest>

这是 Mainactivity.java:

package com.example.inveslator;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(android.R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Second.class));

            }
        });
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I would be glad to know whats wrong. thanks in advance

【问题讨论】:

  • 您是否尝试过运行调试器来查看什么是 null 以及为什么?
  • 不,但我刚刚将 android.R.id.button1 更改为 R.id.button1 作为回答,它对我有用
  • 使用调试器应该是解决任何问题的第一步。如果您在这里使用它,您将能够大大缩小您的问题范围。
  • 当然。下次我会使用调试器。

标签: android xml eclipse


【解决方案1】:

我认为是关于这一行的

Button b = (Button) findViewById(android.R.id.button1);

我猜你的 xml 布局中有一个 Buttonid = button1。在这种情况下使用:

Button b = (Button) findViewById(R.id.button1);

android.R 用于 sdk 资源,R.id.some_id 用于您创建的 ids

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2014-10-30
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多