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

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

Use B_Target_Activity.TARGET_CHOICE_TO_SUB_MESSAGE as key in C_Sub_Target_Activity because you are passing layout id from Activity B_Target_Activity instead of from A_TheList_Activity:
if(intent !=null){
if(intent.containsKey(B_Target_Activity.TARGET_CHOICE_TO_SUB_MESSAGE)){
int layout = intent.getInt(B_Target_Activity.TARGET_CHOICE_TO_SUB_MESSAGE, 0);
setContentView(layout);
}
}

You access wrong key try to replace this code :
int layout = intent.getInt(A_TheList_Activity.LIST_CHOICE_MESSAGE, 0);
With this code :
int layout = intent.getInt(A_TheList_Activity.TARGET_CHOICE_TO_SUB_MESSAGE, 0);

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

Setting up an avatar on android app

I'm wanting to let the user create an avatar (profile picture if you like) when they set up their user info. I've created a method for a single click/touch which would ask the user to take a picture and one for a long click which would ask the user to choose a picture from their gallery.
Below are my methods from the class file:
public void onLaunchCamera(View v) {
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
}
And below is the XML associated with the ImageButton:
<ImageButton
android:id="#+id/ImageButton_Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="#dimen/avatar_size"
android:minHeight="#dimen/avatar_size"
android:onClick="onLaunchCamera"
android:scaleType="fitXY"
android:src="#drawable/avatar"></ImageButton>
All it does is crash when I click on the ImageButton and I have no idea why. Any ideas?
Thanks
EDIT: Adding the logcat below (Sorry about the formatting. Couldn't work out how to get it all sorted properly:
[ 04-12 18:32:50.989 5901: 5901 D/ ]
HostConnection::get() New Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300 Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab, PID: 5901 java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at a android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
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:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438) 
at android.view.View$PerformClick.run(View.java:18422) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.view.View$1.onClick(View.java:3818) 
at android.view.View.performClick(View.java:4438) 
at android.view.View$PerformClick.run(View.java:18422) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 
It looks like you might not have defined avatarButton, if you follow the Caused By path on the LogCat you see the bottom one is a NullPointerException.
Since I can't see the line numbers, the issue is happening on line 201--the only obvious null pointer I see in your code is avatarButton.
Based on what you want to do, you'll want to go about this a bit differently.
Remove the android:onClick="onLaunchCamera" from the XML.
in your onCreate() after you set the content view add the following:
View avatarButton = findViewById(R.id.ImageButton_Avatar);
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
This allows you to set both a click and a longClick listener with more control. The way you had it, you were never really defining the onClick or onLongClick until you clicked on them the first time.

ANDROID JAVA: Activity doesn't start when the used SharedPreferences

I have a problem. I want to read value Integer of activity "Settings" in the service GPSTracker and use it there. I use this SharedPrefrences and confirming key input. If the data is validated that the application returns to class FullscreenActivity.
This is the code responsible for this in activity Settings:
SharedPreferences.Editor editor;
public static final String NAME = "DISTANCE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
SharedPreferences pref = getApplicationContext().getSharedPreferences(NAME, MODE_PRIVATE);
editor=pref.edit();
Edit1= (EditText) findViewById(R.id.editSkan);
accept= (Button) findViewById(R.id.buttonSkan);
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (!Edit1.getText().toString().equals(""))
value = Integer.parseInt(Edit1.getText().toString());
editor.putInt("settings", value);
editor.commit();
Toast.makeText(getApplicationContext(), "Changed, value + " m", Toast.LENGTH_SHORT).show();
finish();
}
});
A code in GPSTracker like this:
SharedPreferences pref;
(...)
private int downloadSettings()
{
pref=context.getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
value = pref.getInt("settings",15);
return value;
}
and a method call:
int dist = downloadSettings();
When I run the apps I get two errors in the log:
*java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adam.mobileproject/com.example.adam.mobileproject.FullscreenActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.adam.mobileproject.GPSTracker.downloadSettings(GPSTracker.java:284)
at com.example.adam.mobileproject.GPSTracker.<init>(GPSTracker.java:200)
at com.example.adam.mobileproject.FullscreenActivity.onCreate(FullscreenActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)*
PLEASE HELP!!!
The problem is definitely that context is null.
However, just like the Activity class, the Service class extends Context, so you should be able to replace context with this or getApplicationContext().
Try this:
private int downloadSettings()
{
pref = getApplicationContext().getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
value = pref.getInt("settings",15);
return value;
}

Null pointer exception - loading a new activity from fragment

I ran into a null pointer exception when trying to load a new activity from a fragment - it essentially tells me an unexpected error has occurred. Below is the log cat message:
08-21 11:57:14.801: E/AndroidRuntime(1245): FATAL EXCEPTION: main
08-21 11:57:14.801: E/AndroidRuntime(1245): Process: com.dooba.beta, PID: 1245
08-21 11:57:14.801: E/AndroidRuntime(1245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.matchOptionActivity}: java.lang.NullPointerException
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.os.Looper.loop(Looper.java:136)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-21 11:57:14.801: E/AndroidRuntime(1245): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 11:57:14.801: E/AndroidRuntime(1245): at java.lang.reflect.Method.invoke(Method.java:515)
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-21 11:57:14.801: E/AndroidRuntime(1245): at dalvik.system.NativeStart.main(Native Method)
08-21 11:57:14.801: E/AndroidRuntime(1245): Caused by: java.lang.NullPointerException
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.dooba.beta.matchOptionActivity.onCreate(matchOptionActivity.java:29)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.Activity.performCreate(Activity.java:5231)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-21 11:57:14.801: E/AndroidRuntime(1245): ... 11 more
Below is the fragment activity code (the place where the new intent activity is called)
public class Fragment1 extends Fragment {
private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ListView usersListView;
private Button logoutButton;
String userGender = ParseUser.getCurrentUser().getString("Gender");
String activityName = ParseUser.getCurrentUser().getString("ActivityName");
Number maxDistance = ParseUser.getCurrentUser().getNumber("Maximum_Distance");
String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");
Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
Number userage = ParseUser.getCurrentUser().getNumber("Age");
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setConversationsList();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
Button newPage = (Button)view.findViewById(R.id.btnMatchConfirm);
newPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), matchOptionActivity.class);
startActivity(intent);
}
});
return view;
}
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
// String userActivitySelectionName = null;
ParseQuery<ParseUser> query = ParseUser.getQuery();
// query.whereEqualTo("ActivityName",userActivitySelectionName);
query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
// users with Gender = currentUser.Looking_Gender
query.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query.whereEqualTo("Looking_Gender", userGender);
query.setLimit(1);
query.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);
query.orderByDescending("Name");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userList, ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).get("Name").toString());
names.add(userList.get(i).get("Headline").toString());
names.add(userList.get(i).get("Age").toString());
names.add(userList.get(i).get("ActivityName").toString());
// names.add(userList.get(i).getParseObject("ProfilePicture").;
}
usersListView = (ListView)getActivity().findViewById(R.id.userlistview1);
namesArrayAdapter =
new ArrayAdapter<String>(getActivity().getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);
usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
}
public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("Name", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getActivity().getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Below is the code for the matchOption activity (the activity that is called upon button click)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matchoption);
final ImageView idrinks = (ImageView) this.findViewById(R.id.icasual);
idrinks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
matchOptionActivity.this.startActivity(new Intent(matchOptionActivity.this, MessagingActivity.class));
}
});
}
}
Furthermore, I am using Parse to populate my list of users, and I would like to know how I would be hide the confirm button in the event that the list is empty.
Thanks in advance.
Update
below is the fragment XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blue_bac3"
android:orientation="vertical" >
<ListView
android:id="#+id/userlistview1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:textColor="#ffffff"
android:divider="#null"
>
</ListView>
<Button
android:id="#+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#drawable/gray_bac"
android:layout_below="#+id/userlistview1"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:text="Confirm" />
</RelativeLayout>
I don't have reputation to add comment...Can you also provide the matchoption xml for further analysis or check the onClick() method. looks like some issue over there
Caused by: java.lang.NullPointerException
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.dooba.beta.matchOptionActivity.onCreate(matchOptionActivity.java:29)
I guess you need to change the code
from this
idrinks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
matchOptionActivity.this.startActivity(new Intent(matchOptionActivity.this, MessagingActivity.class));
}
});
to this
idrinks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this,MessagingActivity.class);
i.startActivity(new Intent(matchOptionActivity.this, MessagingActivity.class));
}
});

How to write to a TextView from outside a button or onCreate

OK,time for another noobie question:
I have declared a TextView at the top of the Activity class as static which I believe should make it "global". Then in onCreate I attach that variable to a TextView. Looks like so:
public class CheckerActivity extends Activity {
String testNumber = "0";
public static TextView displayArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checker);
int[] numbersArray;
Intent intent = getIntent();
numbersArray = intent.getIntArrayExtra(EnterCurrentNumbersActivity.CURRENT_NUMBERS_ARRAY);
String arrayStringed = Arrays.toString(numbersArray);
displayArray = (TextView) findViewById(R.id.currentNumbers);
displayArray.setText(arrayStringed);
}
Then in a method I created myself in the same class I'm trying to use that TextView declaration and write something to it. Like so:
public void addNumber(String[] numbersToAdd) {
this.setContentView(R.layout.activity_checker);
displayArray.setText(Arrays.toString(numbersToAdd));
}
I then have a button that calls the addNumber method like so:
public void populateButton(View view) {
String[] temp = {"test", "array"};
CheckerActivity addNumbers = new CheckerActivity();
addNumbers.addNumber(temp);
}
But when the button is clicked I keep getting NullPointerException on the addNumber method.
I've searched all over for this but I think I just don't know what to ask Google. As far as I can tell, this should work, where am I going wrong??
logcat after your sugestions. (But Eldar Mensutov got it. Works now!)
07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.NullPointerException
07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.addNumber(CheckerActivity.java:85)
07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:37)
07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
To carry of from this question, I now wish to call
public void addNumber(String[] numbersToAdd)
from a completely different class (whole different file within the same project)
Using this.addNumber(temp); doesn't work.
Any suggestions??
Do not call
this.setContentView(R.layout.activity_checker);
in your public void addNumber(String[] numbersToAdd).
And replase
CheckerActivity addNumbers = new CheckerActivity();
addNumbers.addNumber(temp);
on
this.addNumber(temp);
And make you field not static: public TextView displayArray;

Categories