gpt4 book ai didi

java - NullPointerException 关于去另一个 Activity ,android studio

转载 作者:行者123 更新时间:2023-11-30 03:21:26 25 4
gpt4 key购买 nike

更新我发布了新的 LogCat报告,我知道我的第二个名为“Done”的类返回 null 导致此错误:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity 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

最佳答案

在您的第二个 Activity 中,在您的 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

试试这整个代码:

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

完成类(class)

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

关于java - NullPointerException 关于去另一个 Activity ,android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19064834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com