NullPointerException异常在Ecclipse Android SDK中的myapp不幸已经停止 [英] NullPointerException In Ecclipse Android Sdk Unfortunately myapp has stopped

查看:160
本文介绍了NullPointerException异常在Ecclipse Android SDK中的myapp不幸已经停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者。被困在这个NullPointerException异常。这是按钮的一个简单的程序。当我作为运行Android应用程序,项目打开,并立即关闭被通过显示不幸的是我的应用程序已经停止。

  17 04-15:40:05.656:D / AndroidRuntime(599):关闭VM
04-15 17:40:05.656:W / dalvikvm(599):主题ID = 1:螺纹未捕获的异常退出(组= 0xb2adcd58)
04-15 17:40:05.666:E / AndroidRuntime(599):致命异常:主要
04-15 17:40:05.666:E / AndroidRuntime(599):工艺:com.example.buttona,PID:599
04-15 17:40:05.666:E / AndroidRuntime(599):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.buttona / com.example.buttona.Buttonwa}:显示java.lang.NullPointerException
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread.access $ 800(ActivityThread.java:138)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1209)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.os.Looper.loop(Looper.java:136)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:40:05.666:E / AndroidRuntime(599):在java.lang.reflect.Method.invokeNative(本机方法)
04-15 17:40:05.666:E / AndroidRuntime(599):在java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:40:05.666:E / AndroidRuntime(599):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:40:05.666:E / AndroidRuntime(599):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:40:05.666:E / AndroidRuntime(599):在dalvik.system.NativeStart.main(本机方法)
04-15 17:40:05.666:E / AndroidRuntime(599):致:显示java.lang.NullPointerException
04-15 17:40:05.666:E / AndroidRuntime(599):在com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.Activity.performCreate(Activity.java:5231)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:40:05.666:E / AndroidRuntime(599):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:40:05.666:E / AndroidRuntime(599):11 ...更多
04-15 17:40:11.466:I /流程(599):发送信号。 PID:599 SIG:9
04-15 17:57:32.017:D / AndroidRuntime(662):关闭VM
04-15 17:57:32.017:W / dalvikvm(662):主题ID = 1:螺纹未捕获的异常退出(组= 0xb2adcd58)
04-15 17:57:32.027:E / AndroidRuntime(662):致命异常:主要
04-15 17:57:32.027:E / AndroidRuntime(662):工艺:com.example.buttona,PID:662
04-15 17:57:32.027:E / AndroidRuntime(662):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.buttona / com.example.buttona.Buttonwa}:显示java.lang.NullPointerException
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread.access $ 800(ActivityThread.java:138)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1209)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.os.Looper.loop(Looper.java:136)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:57:32.027:E / AndroidRuntime(662):在java.lang.reflect.Method.invokeNative(本机方法)
04-15 17:57:32.027:E / AndroidRuntime(662):在java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:57:32.027:E / AndroidRuntime(662):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:57:32.027:E / AndroidRuntime(662):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:57:32.027:E / AndroidRuntime(662):在dalvik.system.NativeStart.main(本机方法)
04-15 17:57:32.027:E / AndroidRuntime(662):致:显示java.lang.NullPointerException
04-15 17:57:32.027:E / AndroidRuntime(662):在com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.Activity.performCreate(Activity.java:5231)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:57:32.027:E / AndroidRuntime(662):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:57:32.027:E / AndroidRuntime(662):11 ...更多
04-15 17:57:34.957:​​I /流程(662):发送信号。 PID:662 SIG:9

这是Java文件。

 公共类Buttonwa延伸活动{按钮的名称,姓;
TextView中显示;@覆盖
  公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_buttonwa);    名称=(按钮)findViewById(R.id.name_button);
    姓=(按钮)findViewById(R.id.surname_button);
    显示=(的TextView)findViewById(R.id.display);
    name.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根            display.setText(你的名字是沙善亚达夫);        }
    });    surname.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根            display.setText(你的名字是亚达夫沙善);        }
    });

这是XML文件。

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=$ com.example.buttona.Buttonwa PlaceholderFragment><的TextView
    机器人:ID =@ + ID /显示器
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =50dp
     />
<按钮
机器人:ID =@ + ID / name_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizo​​ntal =真
机器人:layout_marginTop =10dp
机器人:文字=@字符串/名称
/><按钮
机器人:ID =@ + ID / surname_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizo​​ntal =真
机器人:layout_below =@ + ID / name_button
机器人:文字=@字符串/姓氏
/>
< / RelativeLayout的>


解决方案

问题是你已经在你的片段创建视图元素 fragment_main.xml (我认为这就是它的所谓,你有没有公布名字),但你的code预计,他们在你的活动。

当你这样做:

 的setContentView(R.layout.activity_buttonwa);名称=(按钮)findViewById(R.id.name_button);
姓=(按钮)findViewById(R.id.surname_button);
显示=(的TextView)findViewById(R.id.display);

您是说:嘿活动,检查 activity_buttonwa.xml 所有这些视图元素,你会在那里找到他们。

从错误信息来看,名称,所以你得到一个 NullPointerException异常当您尝试并在其上设置一个 OnClickListener 。这是,因为它不是在你的 activity_buttonwa.xml 文件。

要解决,你可以将所有的XML转换 activity_buttonwa.xml 您可以将通胀code到您的片段:

  / **
 *包含一个简单视图中的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{    公共PlaceholderFragment(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);        名称=(按钮)rootView.findViewById(R.id.name_button);
        姓=(按钮)rootView.findViewById(R.id.surname_button);
        显示=(TextView中)rootView.findViewById(R.id.display);        //添加OnClickListeners等。        返回rootView;
    }
}

我会说这是好通胀code移动到您的片段,如上面。

I am a beginner. Stuck on this NullPointerException. This is a simple program of buttons. As soon as i run as android application, the project opens and immediately gets closed by displaying unfortunately my app has stopped.

04-15 17:40:05.656: D/AndroidRuntime(599): Shutting down VM
04-15 17:40:05.656: W/dalvikvm(599): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:40:05.666: E/AndroidRuntime(599): FATAL EXCEPTION: main
04-15 17:40:05.666: E/AndroidRuntime(599): Process: com.example.buttona, PID: 599
04-15 17:40:05.666: E/AndroidRuntime(599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.os.Looper.loop(Looper.java:136)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:40:05.666: E/AndroidRuntime(599):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599):  at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:40:05.666: E/AndroidRuntime(599):  at dalvik.system.NativeStart.main(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599): Caused by: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:40:05.666: E/AndroidRuntime(599):  ... 11 more
04-15 17:40:11.466: I/Process(599): Sending signal. PID: 599 SIG: 9
04-15 17:57:32.017: D/AndroidRuntime(662): Shutting down VM
04-15 17:57:32.017: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:57:32.027: E/AndroidRuntime(662): FATAL EXCEPTION: main
04-15 17:57:32.027: E/AndroidRuntime(662): Process: com.example.buttona, PID: 662
04-15 17:57:32.027: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:136)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:57:32.027: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:57:32.027: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662): Caused by: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:57:32.027: E/AndroidRuntime(662):  ... 11 more
04-15 17:57:34.957: I/Process(662): Sending signal. PID: 662 SIG: 9

This Is The Java File.

public class Buttonwa extends Activity {

Button name, surname;
TextView display;

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buttonwa);

    name = (Button) findViewById(R.id.name_button);
    surname = (Button) findViewById(R.id.surname_button);
    display = (TextView) findViewById(R.id.display);
    name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            display.setText("Your Name Is Shashank Yadav");

        }
    });

    surname.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            display.setText("Your Name Is Yadav Shashank");

        }
    });

this is the xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.buttona.Buttonwa$PlaceholderFragment" >

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
     />
<Button 
android:id="@+id/name_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/Name"
/>

<Button 
android:id="@+id/surname_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/name_button"
android:text="@string/Surname"
/>


</RelativeLayout>

解决方案

The problem is you have created View elements in your fragment fragment_main.xml (I assume that's what it's called, you haven't posted the name) but your code expects them in your activity.

When you do this:

setContentView(R.layout.activity_buttonwa);

name = (Button) findViewById(R.id.name_button);
surname = (Button) findViewById(R.id.surname_button);
display = (TextView) findViewById(R.id.display);

You are saying, "Hey activity, check activity_buttonwa.xml for all these view elements, you'll find them there.

Judging from your error message, name is null and therefore you are getting a NullPointerException when you try and set an OnClickListener on it. It's null because it's not in your activity_buttonwa.xml file.

To fix, you can either move all that xml into activity_buttonwa.xml or you can move the inflation code into your fragment:

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        name = (Button) rootView.findViewById(R.id.name_button);
        surname = (Button) rootView.findViewById(R.id.surname_button);
        display = (TextView) rootView.findViewById(R.id.display);

        // Add OnClickListeners, etc.

        return rootView;
    }
}

I would say it's better to move the inflation code to your fragment, as above.

这篇关于NullPointerException异常在Ecclipse Android SDK中的myapp不幸已经停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆