gpt4 book ai didi

java - 显示 int 值时出错 |安卓

转载 作者:行者123 更新时间:2023-12-01 22:54:58 24 4
gpt4 key购买 nike

我正在尝试关注 YouTube 上“The New Boston”的视频系列。尝试在 toast 上显示变量时出现错误。这就是我所做的..

首先,我创建了一个textView..

 <TextView    
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/CountView"
android:text="Number Value is 0"
/>

那么,

public class MainActivity extends ActionBarActivity {
int Counter;
TextView countView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Counter = 0;
countView= (TextView)findViewById(R.id.CountView);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


public void Button1Click(View view) {
Counter+=1;
Toast.makeText(this, "Added 1", Toast.LENGTH_SHORT).show();
countView.setText(String.valueOf(Counter));

}

它说该程序已停止工作。 LogCat日志 ->

 FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3606)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17446)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3601)
... 11 more
Caused by: java.lang.NullPointerException
at com.BreadApplications.program1.MainActivity.Button1Click(MainActivity.java:36)
... 14 more

我刚刚开始学习 Android..有什么帮助吗?

最佳答案

试试这个:

Toast.makeText(this, ""+Counter, Toast.LENGTH_SHORT).show();//auto type conversion really convinent

关于java - 显示 int 值时出错 |安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231097/

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