gpt4 book ai didi

java - Android - Java - Buttonclicker nullpointerException

转载 作者:行者123 更新时间:2023-12-01 21:54:33 25 4
gpt4 key购买 nike

我想做一个按钮点击器。当我按下 btnKopen 时,我希望 textView 'BonnenMand' 显示按钮被点击的次数。

这是我为此使用的代码:

Button btnKopen;
TextView bonnenMand;
int bonnenMandCounter = 0;

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

Button btnKopen = (Button) findViewById(R.id.buttonKopen);
final TextView bonnenMand = (TextView) findViewById(R.id.textViewItemCount);

btnKopen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bonnenMandCounter++;
bonnenMand.setText(bonnenMandCounter);
}
});
}

我收到 NullPointerException 并且应用程序在打开之前崩溃了。

01-04 14:02:48.649 13685-13685/? I/art: Late-enabling -Xcheck:jni
01-04 14:02:48.704 13685-13685/? W/System: ClassLoader referenced unknown path: /data/app/be.ehb.dt.bestellingen-1/lib/arm
01-04 14:02:48.774 13685-13685/? D/AndroidRuntime: Shutting down VM
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: FATAL EXCEPTION: main
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: Process: be.ehb.dt.bestellingen, PID: 13685
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{be.ehb.dt.bestellingen/be.ehb.dt.bestellingen.Winkel}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at be.ehb.dt.bestellingen.Winkel.onCreate(Winkel.java:79)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6251)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
01-04 14:02:48.774 13685-13685/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-04 14:02:51.469 13685-13685/? I/Process: Sending signal. PID: 13685 SIG: 9

编辑:我的 main_activity.xml 中不存在该按钮,使用以下 arrayAdapter 添加按钮..

public class MenuAdapter extends ArrayAdapter<MenuWinkel> {

public MenuAdapter(Context context, ArrayList<MenuWinkel> menus) {
super(context, 0, menus);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

MenuWinkel menu = getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.winkelrow, parent, false);
}

TextView menuName = (TextView) convertView.findViewById(R.id.textViewMenu);
TextView menuPrice = (TextView) convertView.findViewById(R.id.textViewPrijs);

menuName.setText(menu.getNaam());
if(menu.getPrijs() > 0.0) {
StringTokenizer tokens = new StringTokenizer(menu.getPrijs()+"", ".");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();

if (second.equals("0")) {
menuPrice.setText(first);
} else {
menuPrice.setText(first + "," + second);
}

}

return convertView;
}
}

这使我能够放置如下项目:

 ArrayList<MenuWinkel> arrayOfMenus = new ArrayList<MenuWinkel>();
MenuAdapter menuAdapter = new MenuAdapter(this,arrayOfMenus);

MenuWinkel menu1 = new MenuWinkel("10% korting op drank bij menu",50.0);
menuAdapter.add(menu1);
MenuWinkel menu2 = new MenuWinkel("30% korting op drank bij menu",70.0);
menuAdapter.add(menu2);
MenuWinkel menu3 = new MenuWinkel("70% korting op drank bij menu",120.0);

此数组适配器添加具有以下 XML 的项目:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="match_parent"
android:layout_height="106.5dp"
android:id="@+id/imageBackground"
android:src="@drawable/banner_wit"
android:layout_marginTop="4.5dp"
android:contentDescription="@string/banner_white"
android:scaleType="fitXY"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">

<ImageView
android:layout_width="0dp"
android:layout_height="85dp"
android:id="@+id/imageViewMenu"
android:src="@drawable/drinks"
android:contentDescription="Drinks"
android:layout_weight="0.28"
android:layout_marginTop="13dp"
android:layout_marginLeft="-2dp" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewMenu"
android:layout_gravity="center"
android:textColor="#1D1D1B"
android:layout_weight="0.65"
android:paddingLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"/>

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageCaps"
android:src="@drawable/caps"
android:layout_weight="0.01"
android:layout_marginTop="30dp" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textViewPrijs"
android:layout_gravity="center_vertical"
android:textColor="#fff"
android:layout_weight="0.10"
android:layout_marginLeft="-35dp"
android:layout_marginTop="8dp" />

</LinearLayout>

<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Kopen"
android:id="@+id/buttonKopen"
android:textColor="#fff"
android:background="@drawable/borderradiuskopen"
android:layout_marginBottom="20dp"
android:layout_marginLeft="40dp"
android:layout_gravity="center_horizontal|bottom"
/>
</FrameLayout>

在此 XML 中,您可以找到 buttonKopen 的 buttonID。

最佳答案

你的错误是

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

这是由这些行引起的:

Button btnKopen = (Button) findViewById(R.id.buttonKopen);
btnKopen.setOnClickListener(...);

这是因为activity_main.xml中没有包含id为R.id.buttonKopen的按钮

要向按钮添加点击监听器,请在 MenuAdaptergetView 方法中执行此操作。

关于java - Android - Java - Buttonclicker nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34591837/

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