using startActivityForResult - java

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.

Related

NumberFormatException invalid int ""

I am trying to take the input values from EditText and I want to save it to sqlite Database. I dont know how to use the logcat [also please explain how can I read the errors from the LogCat].
MainActivity.java:
package com.example.database;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText first,last,age,classc;
Button add,view;
String FirstName;
String LastName,Class;
Integer Age;
sqLit myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new sqLit(this);
Link();
xmlToVar();
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Integer flag=myDB.insertValue(FirstName, LastName, Class, Age);
if (flag==1)
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Record Added" , Toast.LENGTH_LONG);
toast.show();
}
else
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Error Occured" , Toast.LENGTH_LONG);
toast.show();
}
}
});
}
public void Link()
{
first=(EditText) findViewById(R.id.editFirst);
last=(EditText) findViewById(R.id.editLast);
classc=(EditText) findViewById(R.id.editClass);
age=(EditText) findViewById(R.id.editAge);
add=(Button) findViewById(R.id.Add);
view=(Button) findViewById(R.id.ViewAll);
}
public void xmlToVar()
{
FirstName = first.getText().toString();
LastName = last.getText().toString();
Class = classc.getText().toString();
Age = Integer.parseInt(age.getText().toString());
}
}
sqLit.java:
package com.example.database;
import org.w3c.dom.Text;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class sqLit extends SQLiteOpenHelper
{
public static final String DB_NAME="student12.db";
public static final String TABLE_NAME="class1";
public static final String COL_1="ROLL NO";
public static final String COL_2="First Name";
public static final String COL_3="Last Name";
public static final String COL_4="Class";
public static final String COL_5="Age";
public sqLit(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table " + TABLE_NAME + "(" + COL_1 + "INTEGER AUTOINCREMENT PRIMARY KEY," + COL_2 + "TEXT," + COL_3 + "TEXT," + COL_4 + "TEXT," + COL_5 + "INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exist" + TABLE_NAME );
onCreate(db);
}
public Integer insertValue(String FirstName, String LastName, String Class, Integer Age)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentV = new ContentValues();
contentV.put(COL_2, FirstName);
contentV.put(COL_3, LastName);
contentV.put(COL_4, Class);
contentV.put(COL_5, Age);
long isInserted = db.insert(TABLE_NAME, null, contentV);
if (isInserted == -1){
return 0;
}
else
{
return 1;
}
}
}
Also, please explain how to use LogCat as I don't understand how to read it.
LOGCAT:
06-11 18:44:39.650: E/Trace(29536): error opening trace file: No such file or directory (2)
06-11 18:44:39.660: D/AndroidRuntime(29536): Shutting down VM
06-11 18:44:39.660: W/dalvikvm(29536): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:44:39.660: E/AndroidRuntime(29536): FATAL EXCEPTION: main
06-11 18:44:39.660: E/AndroidRuntime(29536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Looper.loop(Looper.java:137)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:44:39.660: E/AndroidRuntime(29536): at dalvik.system.NativeStart.main(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.onCreate(MainActivity.java:34)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:44:39.660: E/AndroidRuntime(29536): ... 11 more
06-11 18:46:03.652: D/dalvikvm(30256): Not late-enabling CheckJNI (already on)
06-11 18:46:03.682: E/Trace(30256): error opening trace file: No such file or directory (2)
06-11 18:46:03.712: D/AndroidRuntime(30256): Shutting down VM
06-11 18:46:03.712: W/dalvikvm(30256): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:46:03.712: E/AndroidRuntime(30256): FATAL EXCEPTION: main
06-11 18:46:03.712: E/AndroidRuntime(30256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Looper.loop(Looper.java:137)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:46:03.712: E/AndroidRuntime(30256): at dalvik.system.NativeStart.main(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:46:03.712: E/AndroidRuntime(30256): ... 11 more
06-11 18:53:33.378: E/Trace(2383): error opening trace file: No such file or directory (2)
06-11 18:53:33.408: D/AndroidRuntime(2383): Shutting down VM
06-11 18:53:33.408: W/dalvikvm(2383): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:53:33.408: E/AndroidRuntime(2383): FATAL EXCEPTION: main
06-11 18:53:33.408: E/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Looper.loop(Looper.java:137)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:53:33.408: E/AndroidRuntime(2383): at dalvik.system.NativeStart.main(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:53:33.408: E/AndroidRuntime(2383): ... 11 more
Caused by: java.lang.NumberFormatException: Invalid int: ""
(after FATAL EXCEPTION:) tells us you are trying to parse an int but the value you are trying to parse is an empty String.
this line (the first that references your class) tells us which line it was
at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
this is happening because you are calling it in onCreate() before the EditText has any value entered.
From the line above, we can see that the error happens in MainActivity in the method xmlToVar at line 72 which should be
Age = Integer.parseInt(age.getText().toString());
To solve this, you should call that method in an onClick or some event listener. You also should surround it in a try/catch or use some other validation checking.
You should read the following questions and their answers for more details:
What is a stack trace, and how can I use it to debug my application errors?
Unfortunately MyApp has stopped. How can I solve this?
java.lang.NumberFormatException: Invalid int: "" in android
basically, read the error it gives you then look for the first line which mentions your class. Sometimes you need to read deeper but for basic errors that is usually enough.
Also Please Explain how to use LogCat as I am 3 days Old Android Programmer. I never used Eclipse.
Note that the stacktrace isn't Eclipse or even Java/Android specific. You can get some sort of crash log like this in other languages/IDEs/Consoles
The problem is here:
Age = Integer.parseInt(age.getText().toString());
age.getText().toString() equals "" and which causes the NumberFormatException

android app parse values through intent

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);

Failed to pass value to Method retrieve from another method error : java.util.HashSet cannot be cast to java.lang.String

Here is the code mentioned i am trying to insert values in ArrayList and saving arrayList in Session Storage while key is different everytime.Now my target is to get selected value by passing "position" onclick of Spinner.Here are two mthods that are used to save values in SharePreferences i am getting error while trying to retrieve value from getSpinnerSelectedValue method.
public static void setValuesInSession(Context c,String key,List<String> myArrayList)
{
SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor sEdit=sPrefs.edit();
Set<String> set = new HashSet<String>();
Log.i("myArrayList SharePrefereces",""+myArrayList +"::key::"+key);
set.addAll(myArrayList);
sEdit.putStringSet(key,set);
sEdit.commit();
}
public static List<String> getValuesOfSession(Context c,String key)
{
SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor sEdit=sPrefs.edit();
Set<String> set = new HashSet<String>();
set = sPrefs.getStringSet(key,null);
List<String> setList=new ArrayList<String>(set);
return setList;
}
public static void setSpinnerSelectedValue(Context c,String key,String value)
{SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor=pref.edit();
editor.putString(key,value);
}
public static String getSpinnerSelectedValue(Context c,String key)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(c);
String val = sharedPrefs.getString(key,null);
return val;
}
how i am using this code in Activity:
SessionManager.setValuesInSession(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel().toString(),[0100,0200,0300,0400,0500]);
siteSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
Log.i("Spinner Selected Value", "" + position);
List<String> arrayList = new ArrayList<String>();
arrayList = SessionManager.getValuesOfSession(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel());
Log.i("arrayList Values",""+arrayList);
Log.i("arrayList ",""+arrayList.get(position).toString());
SessionManager.setSpinnerSelectedValue(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel(),arrayList.get(position).toString());
}
getting Error when trying to retrieve values from "getSpinnerSelectedValue" mthod ?
here is the error List:
02-11 02:23:05.455: E/AndroidRuntime(23432): FATAL EXCEPTION: main
02-11 02:23:05.455: E/AndroidRuntime(23432): java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.mrfs.android.surveyapp.util.SessionManager.getSpinnerSelectedValue(SessionManager.java:85)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.mrfs.android.surveyapp.activities.fragments.SiteFragmentActivity$3.onClick(SiteFragmentActivity.java:513)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.view.View.performClick(View.java:4211)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.view.View$PerformClick.run(View.java:17267)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Handler.handleCallback(Handler.java:615)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Looper.loop(Looper.java:137)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-11 02:23:05.455: E/AndroidRuntime(23432): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 02:23:05.455: E/AndroidRuntime(23432): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-11 02:23:05.455: E/AndroidRuntime(23432): at dalvik.system.NativeStart.main(Native Method)
onClick of Button i am calling method :
siteSaveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("Session Values",SessionManager.getSpinnerSelectedValue(getActivity().getApplicationContext(),"Police Authority"));
}

Android: Using array adapter to display a list array

I'm not getting a direct error in Eclipse.. but when trying to run this on my phone it doesn't open the activity and then my phone resets. I might be using the array adapter wrong.. but here's my code files:
Java File
package creativecoders.periodictable;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AM extends Activity {
private ListView amList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
#Override
public void onStart() {
super.onStart();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.AM, android.R.layout.simple_list_item_1);
amList.setAdapter(adapter);
}
public void onPause() {
super.onPause();
finish();
}
public void onStop() {
super.onStop();
finish();
}
public void onDestroy() {
super.onDestroy();
finish();
}
}
XML Layout File
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
String File
<string-array name="AM">
<item> ONE </item>
<item> TWO </item>
<item> THREE </item>
</string-array>
EDIT: Log file:
04-20 23:10:07.660: D/AndroidRuntime(284): Shutting down VM
04-20 23:10:07.660: W/dalvikvm(284): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-20 23:10:07.670: E/AndroidRuntime(284): FATAL EXCEPTION: main
04-20 23:10:07.670: E/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{creativecoders.periodictable/creativecoders.periodictable.AM}: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 23:10:07.670: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at creativecoders.periodictable.AM.onStart(AM.java:28)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Activity.performStart(Activity.java:3781)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-20 23:10:07.670: E/AndroidRuntime(284): ... 11 more
you did not initialize the ListView. in onCreate()
amList = (ListView)findViewById(R.id.listView1);

Android application stops unexpectedly when running

when testing my application in the emulator(I'm using Eclipse) it shows me "The application Counter(com.ian.counter) has stopped unexpectedly. Please try again." when running it. I've searched and searched for an answer but I've found none. Anyone know the problem?
Code:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1 = (TextView) this.findViewById(R.id.textView1);
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Logcat:
12-28 16:59:20.615: D/AndroidRuntime(353): Shutting down VM
12-28 16:59:20.686: W/dalvikvm(353): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-28 16:59:20.756: E/AndroidRuntime(353): FATAL EXCEPTION: main
12-28 16:59:20.756: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ian.counter/com.ian.counter.CounterActivity}: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:507)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 16:59:20.756: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): Caused by: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Activity.findViewById(Activity.java:1647)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.ian.counter.CounterActivity.<init>(CounterActivity.java:10)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstance(Class.java:1409)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
Any help is appreciated.
You're trying to set the textView1 variable before the layout has been set. You need to define it as a variable and then assign it in onCreate() after you've called setContentView(). Like this:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) this.findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Just copy and paste the above, that'll work just fine.
You should put this line
textView1 = (TextView) this.findViewById(R.id.textView1);
after
setContentView(R.layout.main);
and you just declare TextView in the begining
TextView textView1;
dont try to use findViewById(R.id.textView1); while declaraing instance variable as view does not exist at that point of time.
This is how your code should look like:
TextView textView1 = null;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}

Categories