Android : Using an intent to chose a specific layout from previous activity - layered

on Tuesday, July 8, 2014


So what I have is a main Activity A_TheList_Activity, from here I move to the next Activity B_Target_Activity with an intent that I put in a specific layout file I created corresponding to the right button the user chose.


That works just fine, However when I try the exact same method with the next activity, the one receiving from the first that will be moving to the third and provide the specific layout corresponding to the button chosen (B_Target_Activity moving to C_Sub_Target_Activity) I receive a NullPointerException. The reason I want to know the specific layout is because I want to be able to identify and give instructions to all the buttons I will put in it.


I do the exact same thing and yet when i get to the second activity and try to go to the third I get a message saying my app has stopped working and it takes me back to the first Activity. Somebody PLEASE help. Also if you know a simpler way to do this that would be easy for a beginning programmer to understand that would be AWESOME TOO!


Here is my code A__TheList_Activity:



public class A_TheList_Activity extends Activity {
public static String LIST_CHOICE_MESSAGE = "THE LIST";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_the_list_layout);


Button Family_Button = (Button) findViewById(R.id.Family_Button);
Family_Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent TargetIntent = new Intent(getApplicationContext(), B_Target_Activity.class);
TargetIntent.putExtra(LIST_CHOICE_MESSAGE, R.layout.target_family_layout);
startActivity(TargetIntent);


}
});

Button Friends_Button = (Button) findViewById(R.id.Friends_Button);
Friends_Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), B_Target_Activity.class);
intent.putExtra(LIST_CHOICE_MESSAGE, R.layout.target_friends_layout);
startActivity(intent);


}
});

Button Love_Button = (Button) findViewById(R.id.Love_Button);
Love_Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), B_Target_Activity.class);
intent.putExtra(LIST_CHOICE_MESSAGE, R.layout.target_love_layout);
startActivity(intent);


}
});

Button Culture_Button = (Button) findViewById(R.id.Culture_Button);
Culture_Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), B_Target_Activity.class);
intent.putExtra(LIST_CHOICE_MESSAGE, R.layout.target_culture_layout);
startActivity(intent);


}
});

}


}


B_Target_Activity:



public class B_Target_Activity extends Activity {
public static String TARGET_CHOICE_TO_SUB_MESSAGE = "Une Target";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle intent = getIntent().getExtras();
Integer layout = intent.getInt(A_TheList_Activity.LIST_CHOICE_MESSAGE, 0);
setContentView(layout);
LayoutChoice(layout);
}



public void LayoutChoice(Integer i){
if(i.equals(R.layout.target_family_layout)){
Button FamilyTargetButton = (Button) findViewById(R.id.FamilyTargetButton);
FamilyTargetButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent SubIntent = new Intent(B_Target_Activity.this, C_Sub_Target_Activity.class);
SubIntent.putExtra(TARGET_CHOICE_TO_SUB_MESSAGE, R.layout.sub_family_layout);
startActivity(SubIntent);
}
});
}else if(i.equals(R.layout.target_friends_layout)){
Button FriendsTargetButton = (Button) findViewById(R.id.FriendsTargetButton);
FriendsTargetButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent SubIntent = new Intent(B_Target_Activity.this, C_Sub_Target_Activity.class);
SubIntent.putExtra(TARGET_CHOICE_TO_SUB_MESSAGE, R.layout.sub_friends_layout);
startActivity(SubIntent);
}
});
}else if(i.equals(R.layout.target_love_layout)){
Button LoveTargetButton = (Button) findViewById(R.id.LoveTargetButton);
LoveTargetButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent SubIntent = new Intent(B_Target_Activity.this, C_Sub_Target_Activity.class);
SubIntent.putExtra(TARGET_CHOICE_TO_SUB_MESSAGE, R.layout.sub_love_layout);
startActivity(SubIntent);

}
});
}else if(i.equals(R.layout.target_culture_layout)){
Button CultureTargetButton = (Button) findViewById(R.id.CultureTargetButton);
CultureTargetButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent SubIntent = new Intent(B_Target_Activity.this, C_Sub_Target_Activity.class);
SubIntent.putExtra(TARGET_CHOICE_TO_SUB_MESSAGE, R.layout.sub_culture_layout);
startActivity(SubIntent);
}
});
}


}


}


C_Sub_Target_Activity, where the NullPointerException happens:



public class C_Sub_Target_Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle intent = getIntent().getExtras();

int layout = intent.getInt(A_TheList_Activity.LIST_CHOICE_MESSAGE, 0);
setContentView(layout);
}


}


Here is my LogCat, Thank you so Much again!!!


07-09 00:02:08.324: D/(1787): HostConnection::get() New Host Connection established 0xb8a7f688, tid 1787 07-09 00:02:08.754: W/EGL_emulation(1787): eglSurfaceAttrib not implemented 07-09 00:02:08.804: D/OpenGLRenderer(1787): Enabling debug mode 0 07-09 00:02:14.394: W/EGL_emulation(1787): eglSurfaceAttrib not implemented 07-09 00:02:17.074: W/ResourceType(1787): No package identifier when getting value for resource number 0x00000000 07-09 00:02:17.074: D/AndroidRuntime(1787): Shutting down VM 07-09 00:02:17.074: W/dalvikvm(1787): threadid=1: thread exiting with uncaught exception (group=0xb3aa3ba8) 07-09 00:02:17.184: E/AndroidRuntime(1787): FATAL EXCEPTION: main 07-09 00:02:17.184: E/AndroidRuntime(1787): Process: com.example.testinttesting, PID: 1787 07-09 00:02:17.184: E/AndroidRuntime(1787): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testinttesting/com.example.testinttesting.C_Sub_Target_Activity}: android.content.res.Resources$NotFoundException: Resource ID #0x0 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread.access$800(ActivityThread.java:135) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.os.Handler.dispatchMessage(Handler.java:102) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.os.Looper.loop(Looper.java:136) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread.main(ActivityThread.java:5017) 07-09 00:02:17.184: E/AndroidRuntime(1787): at java.lang.reflect.Method.invokeNative(Native Method) 07-09 00:02:17.184: E/AndroidRuntime(1787): at java.lang.reflect.Method.invoke(Method.java:515) 07-09 00:02:17.184: E/AndroidRuntime(1787): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-09 00:02:17.184: E/AndroidRuntime(1787): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-09 00:02:17.184: E/AndroidRuntime(1787): at dalvik.system.NativeStart.main(Native Method) 07-09 00:02:17.184: E/AndroidRuntime(1787): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.content.res.Resources.getValue(Resources.java:1123) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.content.res.Resources.getLayout(Resources.java:939) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.view.LayoutInflater.inflate(LayoutInflater.java:395) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 07-09 00:02:17.184: E/AndroidRuntime(1787): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.Activity.setContentView(Activity.java:1929) 07-09 00:02:17.184: E/AndroidRuntime(1787): at com.example.testinttesting.C_Sub_Target_Activity.onCreate(C_Sub_Target_Activity.java:18) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.Activity.performCreate(Activity.java:5231) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 07-09 00:02:17.184: E/AndroidRuntime(1787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 07-09 00:02:17.184: E/AndroidRuntime(1787): ... 11 more 07-09 00:02:21.024: D/(1812): HostConnection::get() New Host Connection established 0xb8a7f770, tid 1812 07-09 00:02:21.584: W/EGL_emulation(1812): eglSurfaceAttrib not implemented 07-09 00:02:21.624: D/OpenGLRenderer(1812): Enabling debug mode 0


0 comments:

Post a Comment