So I'm trying to start a new activity when an image is clicked that will start a new activity with the image being in full screen. To do this I've written the following code:
ImageView defined in XML file:
<ImageView
android:id="#+id/sun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sun_title"
android:layout_centerHorizontal="true"
android:contentDescription="#string/sun_image"
android:src="#drawable/sun" />
Here is where I use an onClickListener to handle the intent when the image is clicked:
ImageView imageView;
//begin onViewCreated Method
...
final Context classContext = null;
imageView = (ImageView) getView().findViewById(R.id.sun);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(classContext, Settings.class);
startActivity(i);
}
});
...
//end onViewCreated Method
I get no errors with the following code but when I run the application and tap the image, the app crashes with a nullPointerException. Here is the logcat for that:
02-25 16:55:43.201: E/AndroidRuntime(3459): FATAL EXCEPTION: main
02-25 16:55:43.201: E/AndroidRuntime(3459): Process: com.andrewq.planets, PID: 3459
02-25 16:55:43.201: E/AndroidRuntime(3459): java.lang.NullPointerException
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.content.ComponentName.<init>(ComponentName.java:77)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.content.Intent.<init>(Intent.java:3821)
02-25 16:55:43.201: E/AndroidRuntime(3459): at com.andrewq.planets.FragmentA$2.onClick(FragmentA.java:43)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.view.View.performClick(View.java:4438)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.view.View$PerformClick.run(View.java:18422)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.os.Handler.handleCallback(Handler.java:733)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.os.Handler.dispatchMessage(Handler.java:95)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.os.Looper.loop(Looper.java:136)
02-25 16:55:43.201: E/AndroidRuntime(3459): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-25 16:55:43.201: E/AndroidRuntime(3459): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 16:55:43.201: E/AndroidRuntime(3459): at java.lang.reflect.Method.invoke(Method.java:515)
02-25 16:55:43.201: E/AndroidRuntime(3459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-25 16:55:43.201: E/AndroidRuntime(3459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-25 16:55:43.201: E/AndroidRuntime(3459): at dalvik.system.NativeStart.main(Native Method)
Hope this is enough information for an answer.
Thanks in advance :)
EDIT: I haven't created the other activity for the image so for testing purposes, I'm having it open my preferences activity.
You have
final Context classContext = null;
so classContext is null. Since it looks like you are in a Fragment, try
Intent i = new Intent(getActivity(), Settings.class);
Your classContext is null. If this code is in Activity then create the Intent like
Intent i = new Intent(this, Settings.class);
If it is from some other class then make sure you pass the valid Context to that class and use it instead of classContext in your code.
Related
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.
I have some trouble with my code and i must have missed something.
here is my code.
I'm trying to build a simple fragment app with one listview window for users. and i can't seem to get it right.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shoppinglist.MainActivity" >
<fragment
android:name="com.example.shoppinglist.CartList"
android:id="#+id/fragment1"
android:layout_width="0px"
android:layout_height="match_parent" />
</RelativeLayout>
CartList code
public class CartList extends ListFragment{
MyArray ma;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
// Create the adapter to convert the array to views
User newUser = new User("Nathan", "San Diego");
adapter.add(newUser);*/
ArrayList<User> arrayOfUsers = new ArrayList<User>();
UsersAdapter adapter = new UsersAdapter(getActivity(), arrayOfUsers);
ListView listView = (ListView) getView().findViewById(R.id.CartListView);
listView.setAdapter(adapter);
}
cartlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/CartListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
UsersAdapter code
public class UsersAdapter extends ArrayAdapter<User> {
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
}
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, R.layout.item_user, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_user, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.name.setText(user.name);
viewHolder.home.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
public class User {
public String name;
public String hometown;
public User(String name, String hometown) {
this.name = name;
this.hometown = hometown;
}
I get this fatal exception
FATAL EXCEPTION: main
02-25 00:39:00.237: E/AndroidRuntime(1912): Process: com.example.shoppinglist, PID: 1912
02-25 00:39:00.237: E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoppinglist/com.example.shoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Handler.dispatchMessage(Handler.java:102)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Looper.loop(Looper.java:135)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Native Method)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Method.java:372)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.setContentView(Activity.java:2144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.MainActivity.onCreate(MainActivity.java:13)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.performCreate(Activity.java:5933)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 10 more
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ' android.view.View android.view.View.findViewById(int)' on a null object reference
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.CartList.onCreate(CartList.java:25)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Fragment.performCreate(Fragment.java:2031)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.onCreateView(Activity.java:5282)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 20 more`
Change
android:name="com.example.shoppinglist.CartList"
To
class="com.example.shoppinglist.CartList"
And make sure your activity is extending FragmentActivity and not Activity
Also make sure your arraylist is not empty. Add item in it in this manner
ArrayList<User> arrayOfUsers = new ArrayList<User>();
User newUser = new User("Nathan", "San Diego");
arrayOfUsers.add(newUser);
I am trying to parse a value through intent while switching between activities.
I know I should read values from last intent with getExtra but I don't know why it doesn't work.
Also when I switch between activities on button click, application crashes.
In activity main I read text from editText and put it in Intent:
public void schimba(View view){
int value = Integer.parseInt(instances.getText().toString());;
Intent intent = new Intent(this, Tabel.class);
intent.putExtra("max", value);
startActivity(intent);
}
When it switch to activity 2 I have this:
Intent intentObject = getIntent();
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = intentObject.getIntExtra("max", 0);
/*
for(i=0;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.activity_tabel, null);
}
*/
setContentView(R.layout.activity_tabel);
TextView showvalue;
showvalue = (TextView) findViewById(R.id.ShowValue);
showvalue.setText(""+value);
The idea is that I want to use this value in a for loop, I already know how to display the value in a textView but I don't need it, I wanna use it in for.
Logcat:
04-23 10:40:52.550: E/AndroidRuntime(1010): FATAL EXCEPTION: main
04-23 10:40:52.550: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instances_temperature/com.example.instances_temperature.Tabel}: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:123)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-23 10:40:52.550: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): Caused by: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.example.instances_temperature.Tabel.onCreate(Tabel.java:26)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-23 10:40:52.550: E/AndroidRuntime(1010): ... 11 more
line 26 would be this:
value = intentObject.getIntExtra("max", 0);
You have to use the code below
int maxValue = getIntent().getExtras().getInt("max");
inside onCreate().
Hope it will solve your problem..
You have not declared intentObject...
Use this:
value = getIntent().getIntExtra("max", 0);
use this
value = getIntent().getStringExtra("max");
instead of this
value = intentObject.getIntExtra("max", 0);
First of all I want to say that this is my first post so it may no be very well done, in general.
My issue is the following: I would like to use three different strings from three editTexts and display them in three TextViews in another activity. I have already been searching for different kinds of ways of doing it (arrays, bundle) but it continues crashing. Help me, please. Here you have my code:
Main activity
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
EditText eText1 = (EditText) findViewById(R.id.editText1);
EditText eText2 = (EditText) findViewById(R.id.editText2);
EditText eText3 = (EditText) findViewById(R.id.editText3);
String m1 = eText1.getText().toString();
String m2 = eText2.getText().toString();
String m3 = eText3.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra("m1",m1);
intent.putExtra("m2",m2);
intent.putExtra("m3",m3);
startActivity(intent);
}
subActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Bundle extras = getIntent().getExtras();
// get the extras
String a = extras.getString("m1");
String b = extras.getString("m2");
String c = extras.getString("m3");
// Set the text views
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(a); // This is line 23
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(b);
TextView tv3 = (TextView) findViewById(R.id.textView3);
tv3.setText(c);
}
Edit1: I have changed what #Squonk said but still crashes. I don't know how to upload the logcat because it is too large for a text but I still can't upload images.
Don't know what to do :(
Edit2: Thanks to #Squonk again. I have finally managed to add the logcat. This is the logcat for the code I recently changed in "Edit1".
03-29 16:05:39.905: E/AndroidRuntime(327): FATAL EXCEPTION: main
03-29 16:05:39.905: E/AndroidRuntime(327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Looper.loop(Looper.java:123)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invoke(Method.java:521)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 16:05:39.905: E/AndroidRuntime(327): at dalvik.system.NativeStart.main(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): Caused by: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 16:05:39.905: E/AndroidRuntime(327): ... 11 more
This is one of the textviews in fragment_display_message.xml. There are three of them. Could it be that when finding the textviews in the subactivity I have to write where to find them not to confuse with the main activity?
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="43dp"
android:hint="#string/edit_message"
android:textAppearance="?android:attr/textAppearanceLarge" />
Edit3: Eureka! I have finally discovered where the problem was. I had inserted the textviews in the fragment_display_message.xml instead of activity_display_message.xml. As in the mainactivity.java I use the fragment_main.xml I thought that in a subactivity would be the same. Well, I was wrong. Thanks to all! :D
Thank you for your support
The problem is these two lines...
Bundle extras = new Bundle();
...
intent.putExtras(extras);
An Intent already carries a Bundle but what you're doing is creating a second Bundle and adding that as an 'extra'.
Remove both of those lines from your main Activity and change the following lines...
extras.putString("m1", m1);
extras.putString("m2", m2);
extras.putString("m3", m3);
...to...
intent.putExtra("m1", m1);
intent.putExtra("m2", m2);
intent.putExtra("m3", m3);
In the first activity,
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("SomeValue", data);
startActivity(i);
// In second activity
String str2 = getIntent().getExtras().getString("SomeValue");
tvRecieve.setText(str2);
i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.