不幸的是,(我的应用名称)已停止

时间:2015-04-14 22:24:09

标签: android xml eclipse

我是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

1 个答案:

答案 0 :(得分:0)

我认为这是关于这一行

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

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

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

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