的更新 的
我发布了新的LogCat报告,我知道有一些与我的第二个类名为“Done”的内容有关,返回null
会导致此错误:
致命的例外:主要 java.lang.RuntimeException:无法启动活动ComponentInfo {com.rocrider.foodmenue / com.rocrider.foodmenue.Done}:java.lang.NullPointerException
我认为问题是在课程Done
与按钮BDone
和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
答案 0 :(得分:2)
在第二项活动中,在onCreate方法中添加对BDone
的调用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
BDone();
}
在课程Main
中,将public class Done extends MainActivity
替换为public class Done extends Activity
。
试试这整个代码:
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();
}
}
}
}
}
答案 1 :(得分:1)
只需完成以下方案即可。您正在调用Done
活动,因此在其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
,因为您正在初始化两个EditTexts。问题出在food1amount
&amp; food2amount
;你从EditText获取文本并解析它。但是你刚刚初始化了那些EditTexts并从中获取文本。这就是为什么你得到 null 。而你正在尝试解析null,以便它显示NullPointerException
。很抱歉,我无法发布整洁的代码,因为我在手机上。我希望你理解。
答案 2 :(得分:0)
您的问题在于手动创建第二个活动,因为onCreate没有被调用,并且您得到NullPoinerException。应在完成活动中从onCreate调用BDone()
。
答案 3 :(得分:0)
在MainActivity中的OnClick中,您正在调用导致错误的done.BDone();
。因为那时布局没有在Done活动中膨胀,所以findViewById会抛出错误。在完成活动中从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));
}
});
}
}
在完成课程中,请执行以下操作:
public class Done extends MainActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
BDone();
}
答案 4 :(得分:0)
您的超级完成活动是MainActivity
。它应该是Activity
。
替换
public class Done extends MainActivity
到
public class Done extends Activity
答案 5 :(得分:-2)
Button bdone = (Button)findViewById(id of button);
bdone.setOnClickListener(new OnlickListener)
{
@Override
public void onClick(View view)
{
//Code here
}
}