更新
我发布了新的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