NullPointerException异常要去另一个活动,机器人工作室 [英] NullPointerException on going to another activity, android studio

查看:118
本文介绍了NullPointerException异常要去另一个活动,机器人工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新
我张贴新的 LogCat中报道,我知道有我的相关名为二等东西完成返回这是造成这个错误:


  

致命异常:主要
          了java.lang.RuntimeException:无法启动活动ComponentInfo {com.rocrider.foodmenue / com.rocrider.foodmenue.Done}:显示java.lang.NullPointerException


我认为这个问题是某处类完成的关联,与按钮 BDONE 和second_activity <一个HREF =htt​​p://en.wikipedia.org/wiki/XML相对=nofollow> XML文件

只是为了信息,我做给大于0值food1amount和food2amount我的应用程序启动时。我按下按钮BDONE后,它崩溃。

此应用程序被认为是对服务员拿在平板电脑上,从客户的订单,然后该程序应该使有序大于0的有大量的食物清单,后来,在此就不code 它应该能够将其发送到打印机在餐厅的厨房里。

我的主要命名MainActivity类别:

 包com.rocrider.foodmenue;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        按钮BDONE =(按钮)findViewById(R.id.Bdone);
        Bdone.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                startActivity(新意图(MainActivity.this,Done.class));
            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }
}

命名完成后,

我的第二类理应与second_activity.xml相关的:

 包com.rocrider.foodmenue;进口android.os.Bundle;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.EditText;
进口android.widget.ListView;进口的java.util.ArrayList;
完成公共类扩展MainActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.second_activity);
        完成();
    }    公共无效BDONE(){
        的EditText Tfood1amount =(EditText上)findViewById(R.id.TFood1Amount);
        的EditText Tfood2amount =(EditText上)findViewById(R.id.TFood2Amount);
        ListView控件lvlast =(ListView控件)findViewById(R.id.LVfinal);
        最终的ArrayList&LT;串GT; OrderList =新的ArrayList&LT;串GT;();
        最后ArrayAdapter&LT;串GT;适配器;
        INT food1amount = 0;
        INT food2amount = 0;
        food1amount =的Integer.parseInt(将String.valueOf(Tfood1amount.getText()));
        food2amount =的Integer.parseInt(将String.valueOf(Tfood2amount.getText()));        适配器=新ArrayAdapter&LT;串GT;(这一点,R.id.LVfinal,OrderList);
        lvlast.setAdapter(适配器);        如果(food1amount!= 0){
            类alaki {
                公共无效加法器(视图v){
                    OrderList.add(食物1:);
                    adapter.notifyDataSetChanged();
                }
            }
        }        如果(food2amount!= 0){
            类alaki {
                公共无效加法器(视图v){
                    OrderList.add(FOOD2:);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}

我的Andr​​oidManifest.xml文件:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.rocrider.foodmenue
    安卓版code =1
    机器人:=的versionName1.0&GT;    &LT;用途-SDK
        安卓的minSdkVersion =7
        机器人:targetSdkVersion =16/&GT;    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名字=com.rocrider.foodmenue.MainActivity
            机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
        &LT;活动机器人:完成NAME = /&GT;
    &LT; /用途&gt;
&LT; /清单&GT;

最新LogCat中报告:

  10月9日至29日:40:58.976 15228-15228 / com.rocrider.foodmenue E / AndroidRuntime?致命异常:主要
    了java.lang.RuntimeException:无法启动活动ComponentInfo {com.rocrider.foodmenue / com.rocrider.foodmenue.Done}:显示java.lang.NullPointerException
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:123)
    在android.app.ActivityThread.main(ActivityThread.java:3687)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:507)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:842)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    在dalvik.system.NativeStart.main(本机方法)
    显示java.lang.NullPointerException:产生的原因
    在com.rocrider.foodmenue.Done.BDone(Done.java:37)
    在com.rocrider.foodmenue.Done.onCreate(Done.java:19)
    在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11更多
    09-29 10:40:58.980 118-136 /? E /? Dumpstate&GT; /数据/日志/ dumpstate_app_error


解决方案

刚刚经历以下情形。你打电话完成活动,以便在其的onCreate 方法你调用 BDONE() 功能。在该功能,

 的EditText Tfood1amount =(EditText上)findViewById(R.id.TFood1Amount);
的EditText Tfood2amount =(EditText上)findViewById(R.id.TFood2Amount);
ListView控件lvlast =(ListView控件)findViewById(R.id.LVfinal);
最终的ArrayList&LT;串GT; OrderList =新的ArrayList&LT;串GT;();
最后ArrayAdapter&LT;串GT;适配器;
INT food1amount = 0;
INT food2amount = 0;
food1amount =的Integer.parseInt(将String.valueOf(Tfood1amount.getText()));
food2amount =的Integer.parseInt(将String.valueOf(Tfood2amount.getText()));

这些行可能会被扔 NullPointerException异常,因为你初始化2 EditTexts。问题出在 food1amount &安培; food2amount ;你从获得的EditText文字和解析它。但你只是初始化这些EditTexts并从中获取文本。这就是为什么你得到的无效即可。而你试图解析空,所以它呈现出 NullPointerException异常。对不起,我不能张贴整齐code因为我在移动。我希望你明白。

update I posted the new LogCat reports, and I know there is something related to my second class named "Done" returning null which is causing this error:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rocrider.foodmenue/com.rocrider.foodmenue.Done}: java.lang.NullPointerException

I think the problem is somewhere in association of the class Done, with the button BDone and second_activity XML file.

Just for information, I do give more than 0 values to food1amount and food2amount when my app starts. After I push the button "BDone", it crashes.

This app is supposed to be for waiters and waitresses to take orders from customers on tablets and then the program is supposed to make a list of foods with amounts ordered more than 0, "and later, not in this code" it is supposed to be able to send it to the printer in restaurant's kitchen.

My main class named MainActivity:

package com.rocrider.foodmenue;

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 {

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

        Button Bdone = (Button) findViewById(R.id.Bdone);
        Bdone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Done.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;
    }
}

My second class named Done, supposedly associated with second_activity.xml:

package com.rocrider.foodmenue;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class Done extends MainActivity{

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

    public void BDone(){
        EditText Tfood1amount = (EditText) findViewById(R.id.TFood1Amount);
        EditText Tfood2amount = (EditText) findViewById(R.id.TFood2Amount);
        ListView lvlast = (ListView) findViewById(R.id.LVfinal);
        final ArrayList<String> OrderList = new ArrayList<String>();
        final ArrayAdapter<String> adapter;
        int food1amount = 0;
        int food2amount = 0;
        food1amount = Integer.parseInt(String.valueOf(Tfood1amount.getText()));
        food2amount = Integer.parseInt(String.valueOf(Tfood2amount.getText()));

        adapter = new ArrayAdapter<String>(this,R.id.LVfinal,OrderList);
        lvlast.setAdapter(adapter);

        if(food1amount != 0){
            class alaki{
                public void adder(View v){
                    OrderList.add("Food1: ");
                    adapter.notifyDataSetChanged();
                }
            }
        }

        if(food2amount != 0){
            class alaki{
                public void adder(View v){
                    OrderList.add("Food2: ");
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}

My androidmanifest.xml file:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

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

Latest LogCat report:

09-29 10:40:58.976  15228-15228/com.rocrider.foodmenue E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rocrider.foodmenue/com.rocrider.foodmenue.Done}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.rocrider.foodmenue.Done.BDone(Done.java:37)
    at com.rocrider.foodmenue.Done.onCreate(Done.java:19)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more
    09-29 10:40:58.980      118-136/? E/? Dumpstate > /data/log/dumpstate_app_error

解决方案

Just go through the following scenario. You're calling Done Activity so in its onCreate method you're calling BDone() function. In that function,

EditText Tfood1amount = (EditText) findViewById(R.id.TFood1Amount);
EditText Tfood2amount = (EditText) findViewById(R.id.TFood2Amount);
ListView lvlast = (ListView) findViewById(R.id.LVfinal);
final ArrayList<String> OrderList = new ArrayList<String>();
final ArrayAdapter<String> adapter;
int food1amount = 0;
int food2amount = 0;
food1amount = Integer.parseInt(String.valueOf(Tfood1amount.getText()));
food2amount = Integer.parseInt(String.valueOf(Tfood2amount.getText()));

These lines might be throwing NullPointerException, because you're initialising two EditTexts. The problem is in food1amount & food2amount; you're getting text from EditText and parsing it. But you just initialized those EditTexts and getting text from it. That is why you're getting null. And you're trying to parse null, so that it's showing a NullPointerException. Sorry that I can't post neat code as I'm on a mobile. I hope you understood.

这篇关于NullPointerException异常要去另一个活动,机器人工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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