gpt4 book ai didi

java - setImageResource 崩溃程序

转载 作者:行者123 更新时间:2023-11-29 07:55:30 26 4
gpt4 key购买 nike

每当我尝试运行该行时,我的程序就会崩溃:

d1.setImageResource(R.drawable.clubs9);

我想不通为什么。它似乎与我使用类似代码行的另一个程序相同。我试过添加 getResources().getDrawable(),但它不会编译。我完全卡住了。任何帮助将不胜感激!


public class TCP extends Activity {
private List<Card> cards;
private ImageButton deal;
private ImageButton fold;
private ImageView d1;
private ImageView d2;
private ImageView d3;
private ImageView p1;
private ImageView p2;
private ImageView p3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tcp);
createDeck();
d1.setImageResource(R.drawable.clubs9);
}

XML

<ImageView
android:id="@+id/d1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/p1"
android:layout_alignTop="@+id/d2"
android:src="@drawable/diamondsq" />

洛卡特:

08-04 12:11:49.154: E/AndroidRuntime(4709): FATAL EXCEPTION: main
08-04 12:11:49.154: E/AndroidRuntime(4709): java.lang.IllegalStateException: Could not execute method of the activity
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.view.View$1.onClick(View.java:3599)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.view.View.performClick(View.java:4204)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.view.View$PerformClick.run(View.java:17355)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.os.Handler.handleCallback(Handler.java:725)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.os.Looper.loop(Looper.java:137)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-04 12:11:49.154: E/AndroidRuntime(4709): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 12:11:49.154: E/AndroidRuntime(4709): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 12:11:49.154: E/AndroidRuntime(4709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-04 12:11:49.154: E/AndroidRuntime(4709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-04 12:11:49.154: E/AndroidRuntime(4709): at dalvik.system.NativeStart.main(Native Method)
08-04 12:11:49.154: E/AndroidRuntime(4709): Caused by: java.lang.reflect.InvocationTargetException
08-04 12:11:49.154: E/AndroidRuntime(4709): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 12:11:49.154: E/AndroidRuntime(4709): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 12:11:49.154: E/AndroidRuntime(4709): at android.view.View$1.onClick(View.java:3594)
08-04 12:11:49.154: E/AndroidRuntime(4709): ... 11 more
08-04 12:11:49.154: E/AndroidRuntime(4709): Caused by: java.lang.NullPointerException
08-04 12:11:49.154: E/AndroidRuntime(4709): at com.evorlor.threecardpoker.TCP.deal(TCP.java:64)
08-04 12:11:49.154: E/AndroidRuntime(4709): ... 14 more

最佳答案

java.lang.NullPointerException because your d1 is null

所以用

d1=(ImageView)findViewById(R.id.d1);

所以你的代码应该是

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tcp);
createDeck();
d1=(ImageView)findViewById(R.id.d1);
d1.setImageResource(R.drawable.clubs9);
}

关于java - setImageResource 崩溃程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18044985/

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