1

更新 我发布了新的LogCat报告,我知道有一些与我的第二个名为“Done”的类相关的东西返回null,这导致了这个错误:

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

我认为问题出在 class 关联的某个地方Done,与 buttonBDone和 second_activity XML文件有关。

仅供参考,当我的应用程序启动时,我确实为 food1amount 和 food2amount 提供了超过 0 个值。在我按下“BDone”按钮后,它崩溃了。

这个应用程序应该是让服务员和女服务员在平板电脑上接受顾客的订单,然后程序应该列出订购量超过 0 的食物,“后来,不在这个代码中”它应该是能够将其发送到餐厅厨房的打印机。

我的主类名为 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;
    }
}

我的第二个类名为 Done,据说与 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();
                }
            }
        }
    }
}

我的 androidmanifest.xml 文件:

<?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>

最新的 LogCat 报告:

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
4

6 回答 6

2

BDone在您的第二个活动中,在您的 onCreate 方法中添加一个调用:

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

在课堂Main上,替换public class Done extends MainActivitypublic class Done extends Activity.

试试这整个代码:

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);
        final Done done = new Done();
        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.activity_main, menu);
        return true;
    }
}

完成课程

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 Activity{

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

    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();
                }
            }
        }
    }
}
于 2013-09-28T08:30:11.060 回答
1

只需通过以下场景。您正在调用DoneActivity,因此在其onCreate方法中您正在调用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()));

这些行可能会抛出NullPointerException,因为您正在初始化两个 EditText。问题在于food1amount& food2amount; 您正在从 EditText 获取文本并对其进行解析。但是您只是初始化了那些 EditTexts 并从中获取文本。这就是为什么你得到null。而且您正在尝试解析 null,以便它显示NullPointerException. 抱歉,我在手机上无法发布整洁的代码。我希望你明白。

于 2013-09-29T08:16:23.133 回答
0

您的问题在于手动创建第二个活动,因为没有调用 onCreate,并且您会收到 NullPoinerException。应该从Done活动BDone()中的 onCreate 调用。

于 2013-09-28T08:20:54.170 回答
0

在 MainActivity 的 OnClick 中,您正在调用done.BDone();导致错误的方法。因为当时布局还没有在 Done 活动中膨胀,所以 findViewById 是在抛出错误。从 Done 活动中的 onCreate调用BDone

在主类中,执行以下操作:

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

在 Done 类中,执行以下操作:

public class Done extends MainActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        BDone();
    }
于 2013-09-28T08:31:43.867 回答
0

你完成活动的超类是MainActivity。应该是Activity

代替

public class Done extends MainActivity

public class Done extends Activity
于 2013-09-28T08:58:57.243 回答
-2
  Button bdone = (Button)findViewById(id of button);
  bdone.setOnClickListener(new OnlickListener)
  {
      @Override
      public void onClick(View view)
      {
          //Code here
      }
  }
于 2013-09-28T08:34:11.470 回答