尝试添加按钮时出错

时间:2011-12-07 19:37:22

标签: android button

当我尝试在我的AVD中运行它时,它会给出一个错误,指出: 抱歉!应用程序Hello World(进程com.duncan.hello.world)意外停止。请再试一次。 我认为它与添加第二个按钮的代码有关,因为它在此之前工作正常。这是我在主java文件中的代码:

package com.duncan.hello.world;  

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button;  
import com.duncan.hello.world.R;  

public class HelloWorldActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) {     
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.main);     

              Button aButton;
    aButton = (Button) this.findViewById(R.id.button1);

    aButton.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {             

Intent i = new Intent (HelloWorldActivity.this, OtherActivity.class);             
            startActivity(i);       
            }
        }); 

    Button newButton;

    newButton = (Button) this.findViewById(R.id.meh);

    newButton.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {             
Intent i = new Intent(HelloWorldActivity.this, Meh.class);             
            startActivity(i);       
            }
        });

    } 
}

这是other.xml(meh所在的布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="you...are...on...page...2...!!!" />

<Button
    android:id="@+id/meh"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="meh"/>


<Button
    android:id="@+id/p40"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="this go to p40" />

</LinearLayout>

这是Meh.java:

package com.duncan.hello.world;
import com.duncan.hello.world.R;

import android.app.Activity;
import android.os.Bundle;
public class Meh extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meh);
}
}

logcat错误: 12-07 14:40:00.840:E / AndroidRuntime(497):致命异常:主

12-07 14:40:00.840:E / AndroidRuntime(497):java.lang.RuntimeException:无法启动活动ComponentInfo {com.duncan.hello.world/com.duncan.hello.world.HelloWorldActivity}:显示java.lang.NullPointerException

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.os.Handler.dispatchMessage(Handler.java:99)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.os.Looper.loop(Looper.java:123)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread.main (ActivityThread.java:4627)

12-07 14:40:00.840:E / AndroidRuntime(497):at java.lang.reflect.Method.invokeNative(Native Method)

12-07 14:40:00.840:E / AndroidRuntime(497):at java.lang.reflect.Method.invoke(Method.java:521)

12-07 14:40:00.840:E / AndroidRuntime(497):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)

12-07 14:40:00.840:E / AndroidRuntime(497):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

12-07 14:40:00.840:E / AndroidRuntime(497):at dalvik.system.NativeStart.main(Native Method)

12-07 14:40:00.840:E / AndroidRuntime(497):引起:java.lang.NullPointerException

12-07 14:40:00.840:E / AndroidRuntime(497):at com.duncan.hello.world.HelloWorldActivity.onCreate(HelloWorldActivity.java:32)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

12-07 14:40:00.840:E / AndroidRuntime(497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

12-07 14:40:00.840:E / AndroidRuntime(497):... 11更多

2 个答案:

答案 0 :(得分:0)

你能给你的logcat输出吗?如果在添加第二个按钮后出现问题,那么是因为Meh.java不在你的mainfest文件中吗?

如果将视图(右上角)切换到DDMS,然后单击LogCat,则可以找到Logcat输出。单击logcat窗口右侧的W或E,它应显示发生的警告和错误。你的崩溃将是红色,所以复制粘贴该部分以向我们展示问题。

你的问题是你要宣布

Button aButton; 

在调用super onCreate方法之前。在执行任何其他操作之前,必须先调用super.onCreate(bundleInstanceState)。

你想做的更像是:

public class HelloWorldActivity extends Activity { 
    Button aButton;

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //whatever button listeners you want to create and initialize buttons
     }
 }

你提到了这个&#39; meh&#39;按钮位于other.xml中。它应该在main.xml中。添加一个ID为&#39; meh&#39;在main.xml中。

答案 1 :(得分:0)

问题是您的内容视图是main.xml - 而不是other.xml。如果你的'meh'按钮不在main.xml中,你将得到一个NullPointerException。