My program is crashing whenever I try to run the line:
d1.setImageResource(R.drawable.clubs9);
I cannot figure out why. It seems identical to another program I have that works using a similar line of code. I have tried adding getResources().getDrawable(), but then it will not compile. I am completely stuck. Any help would be greatly appreciated!
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" />
LOGCAT:
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
so use
d1=(ImageView)findViewById(R.id.d1);
so your code should be
@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);
}
Similar problem here.
If you are calling it from onCreate, make sure you are calling setImageResource AFTER setContentView.
onCreate:
super.onCreate(savedInstanceState);
this.setContentView(R.layout.myAct);
myBtn = (ImageButton) findViewById(R.id.myBtn);
myBtn.setImageResource(R.drawable.mypng);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.