I am trying to make a service and a notification for that. What I have is two buttons. clicking on start button starts should start service and stop should stop. But right now they are not working. I have a main class, service class and notification class. here are my code and logcat output.
My service class is here
package com.zafar.batterynotify;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class BatteryService extends Service {
Notify notification = new Notify();
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
notification.initNotification(this);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public void onDestroy() {
super.onDestroy();
notification.cancelNotification(this);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
}
Notification class
package com.zafar.batterynotify;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class Notify {
private static final int NOTIFICATION_ID = 1;
public void initNotification(Context actContext) {
String ns = actContext.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) actContext.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Service Started";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
//Context context = actContext.getApplicationContext();
Context context = MyApplication.getContext();
CharSequence contentTitle = "Ongoing service";
CharSequence contentText = "This is service is ongoing";
Intent notificationIntent = new Intent(context, BatteryNotify.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
public void cancelNotification(Context actContext) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) actContext.getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
My logcat output
05-12 12:35:35.480: D/AndroidRuntime(27598): Shutting down VM
05-12 12:35:35.480: W/dalvikvm(27598): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-12 12:35:35.490: E/AndroidRuntime(27598): FATAL EXCEPTION: main
05-12 12:35:35.490: E/AndroidRuntime(27598): java.lang.RuntimeException: Unable to start service com.zafar.batterynotify.BatteryService#40526a48 with Intent { cmp=com.zafar.batterynotify/.BatteryService }: java.lang.NullPointerException
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.os.Looper.loop(Looper.java:130)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-12 12:35:35.490: E/AndroidRuntime(27598): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:35:35.490: E/AndroidRuntime(27598): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 12:35:35.490: E/AndroidRuntime(27598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-12 12:35:35.490: E/AndroidRuntime(27598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-12 12:35:35.490: E/AndroidRuntime(27598): at dalvik.system.NativeStart.main(Native Method)
05-12 12:35:35.490: E/AndroidRuntime(27598): Caused by: java.lang.NullPointerException
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.content.ComponentName.<init>(ComponentName.java:75)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.content.Intent.<init>(Intent.java:2859)
05-12 12:35:35.490: E/AndroidRuntime(27598): at com.zafar.batterynotify.Notify.initNotification(Notify.java:24)
05-12 12:35:35.490: E/AndroidRuntime(27598): at com.zafar.batterynotify.BatteryService.onStartCommand(BatteryService.java:20)
05-12 12:35:35.490: E/AndroidRuntime(27598): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
05-12 12:35:35.490: E/AndroidRuntime(27598): ... 10 more
05-12 12:35:56.465: D/AndroidRuntime(27706): Shutting down VM
05-12 12:35:56.465: W/dalvikvm(27706): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-12 12:35:56.470: E/AndroidRuntime(27706): FATAL EXCEPTION: main
05-12 12:35:56.470: E/AndroidRuntime(27706): java.lang.RuntimeException: Unable to start service com.zafar.batterynotify.BatteryService#4051f2f8 with Intent { cmp=com.zafar.batterynotify/.BatteryService }: java.lang.NullPointerException
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.os.Looper.loop(Looper.java:130)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-12 12:35:56.470: E/AndroidRuntime(27706): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:35:56.470: E/AndroidRuntime(27706): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 12:35:56.470: E/AndroidRuntime(27706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-12 12:35:56.470: E/AndroidRuntime(27706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-12 12:35:56.470: E/AndroidRuntime(27706): at dalvik.system.NativeStart.main(Native Method)
05-12 12:35:56.470: E/AndroidRuntime(27706): Caused by: java.lang.NullPointerException
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.content.ComponentName.<init>(ComponentName.java:75)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.content.Intent.<init>(Intent.java:2859)
05-12 12:35:56.470: E/AndroidRuntime(27706): at com.zafar.batterynotify.Notify.initNotification(Notify.java:24)
05-12 12:35:56.470: E/AndroidRuntime(27706): at com.zafar.batterynotify.BatteryService.onStartCommand(BatteryService.java:20)
05-12 12:35:56.470: E/AndroidRuntime(27706): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
05-12 12:35:56.470: E/AndroidRuntime(27706): ... 10 more
In this line:
Intent notificationIntent = new Intent(context, BatteryNotify.class);
context is null.
Related
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
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);
My application crashes whenever I launch it! whats wrong with it? I have created a database and I am trying to populate my listview from the database I have created what is the problem with the code and what is the fix for it?
02-19 22:02:07.502: D/AndroidRuntime(330): Shutting down VM
02-19 22:02:07.502: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:02:07.542: E/AndroidRuntime(330): FATAL EXCEPTION: main
02-19 22:02:07.542: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:02:07.542: E/AndroidRuntime(330): ... 11 more
02-19 22:10:47.354: D/AndroidRuntime(364): Shutting down VM
02-19 22:10:47.444: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:10:47.482: E/AndroidRuntime(364): FATAL EXCEPTION: main
02-19 22:10:47.482: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Looper.loop(Looper.java:123)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.NativeStart.main(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:10:47.482: E/AndroidRuntime(364): ... 11 more
02-19 22:11:10.302: I/Process(364): Sending signal. PID: 364 SIG: 9
02-19 22:11:17.712: D/AndroidRuntime(413): Shutting down VM
02-19 22:11:17.712: W/dalvikvm(413): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:11:17.852: E/AndroidRuntime(413): FATAL EXCEPTION: main
02-19 22:11:17.852: E/AndroidRuntime(413): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:11:17.852: E/AndroidRuntime(413): ... 11 more
02-19 22:11:19.852: I/Process(413): Sending signal. PID: 413 SIG: 9
Activity:
package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity {
private DBHelper mySQLiteAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("A for Apply");
mySQLiteAdapter.insert("B for Boy");
mySQLiteAdapter.insert("C for Cat");
mySQLiteAdapter.insert("D for Dog");
mySQLiteAdapter.insert("E for Egg");
mySQLiteAdapter.insert("F for Fish");
mySQLiteAdapter.insert("G for Girl");
mySQLiteAdapter.insert("H for Hand");
mySQLiteAdapter.insert("I for Ice-scream");
mySQLiteAdapter.insert("J for Jet");
mySQLiteAdapter.insert("K for Kite");
mySQLiteAdapter.insert("L for Lamp");
mySQLiteAdapter.insert("M for Man");
mySQLiteAdapter.insert("N for Nose");
mySQLiteAdapter.insert("O for Orange");
mySQLiteAdapter.insert("P for Pen");
mySQLiteAdapter.insert("Q for Queen");
mySQLiteAdapter.insert("R for Rain");
mySQLiteAdapter.insert("S for Sugar");
mySQLiteAdapter.insert("T for Tree");
mySQLiteAdapter.insert("U for Umbrella");
mySQLiteAdapter.insert("V for Van");
mySQLiteAdapter.insert("W for Water");
mySQLiteAdapter.insert("X for X'mas");
mySQLiteAdapter.insert("Y for Yellow");
mySQLiteAdapter.insert("Z for Zoo");
mySQLiteAdapter.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{DBHelper.KEY_CONTENT};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Database Class
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public DBHelper(Context c){
context = c;
}
public DBHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DBHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
you are Tryin to load MainActivity
java.lang.ClassNotFoundException: com.example.database.MainActivity
inside your Manifest.xml change MainActivity to AndroidSQLite.
And you must use an Asynctask for the Database operations, to avoid ANR Dialog.
I am trying to make a listview and when i click to an item i want to opwn another class. The second class is actually for opening a file from raw folder.
my class is
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Cod_rutier extends MainActivity2A {
private ListView lv1;
private String lv_arr[]={"CAP.1: Dispozitii generale",
"CAP.2: Vehiculele",
"CAP.3: Conducatorii de vehicule",
"CAP.4: Semnalizarea rutiera",
"CAP.5: Reguli de circulatie",
"CAP.6: Infractiuni si pedepse",
"CAP.7: Raspunderea contraventionala",
"CAP.8: Cai de atac impotriva procesului-verbal de constatare a contraventiei",
"CAP.9: Atributii ale unor ministere si ale altor autoritati ale administratiei publice",
"CAP.10: Dispozitii finale"};
/** Called when the activity is first created. */
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.legislatie);
lv1=(ListView)findViewById(R.id.listView1);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final TextView mTextView = (TextView)view;
switch (position) {
case 0:
Intent newActivity0 = new Intent(Cod_rutier.this, Legislatie1.class);
startActivity(newActivity0);
break;
case 1:
Intent newActivity1 = new Intent(Cod_rutier.this, Legislatie2.class);
startActivity(newActivity1);
break;
case 2 :
Intent newActivity2 = new Intent(Cod_rutier.this, Legislatie3.class);
startActivity(newActivity2);
break;
case 3:
Intent newActivity3 = new Intent(Cod_rutier.this, Legislatie4.class);
startActivity(newActivity3);
break;
default:
}
}
});
}}
i put it into the manifest file but when i run it the app stops. This is my logcat
03-16 18:58:35.373: E/AndroidRuntime(828): FATAL EXCEPTION: main
03-16 18:58:35.373: E/AndroidRuntime(828): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vreaucarnet/com.example.vreaucarnet.Cod_rutier}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:123)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 18:58:35.373: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 18:58:35.373: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 18:58:35.373: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 18:58:35.373: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 18:58:35.373: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
03-16 18:58:35.373: E/AndroidRuntime(828): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-16 18:58:35.373: E/AndroidRuntime(828): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.Activity.requestWindowFeature(Activity.java:2719)
03-16 18:58:35.373: E/AndroidRuntime(828): at com.example.vreaucarnet.Cod_rutier.onCreate(Cod_rutier.java:33)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 18:58:35.373: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-16 18:58:35.373: E/AndroidRuntime(828): ... 11 more
Can someone tell my where is my mistake?
Requesting any requestWindowFeature calls are done before super.onCreate calls in your activity
i.e
requestWindowFeature(Window.FEATURE_NO_TITLE); this should be called before super.onCreate()
change like
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
It is here:
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
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.