How to solve NullPointerException in Bitmap decleration. - java

I'm getting an error here that says,
at android.graphics.Bitmap_Delegate.nativeWidth(Bitmap_Delegate.java:291)
at android.graphics.Bitmap.nativeWidth(Bitmap.java)
at android.graphics.Bitmap.getWidth(Bitmap.java:785)
The DDMS says I'm getting an error at line 66 of the ViewingWindow class which is where the int bitmapX is declared and initialized:
protected void onDraw(Canvas canvas) {
if (canvas == null) throw new NullPointerException();
Bitmap bitmap = null;
if (Monitor.getCurrent() == Monitor.TYPE.GREEN) bitmap = greenBitmap;
else bitmap = redBitmap;
int bitmapX = bitmap.getWidth() / 2;
int bitmapY = bitmap.getHeight() / 2;
int parentX = parentWidth / 2;
int parentY = parentHeight / 2;
int centerX = parentX - bitmapX;
int centerY = parentY - bitmapY;
matrix.reset();
matrix.postTranslate(centerX, centerY);
canvas.drawBitmap(bitmap, matrix, paint);
}
Why am I getting such an error? Any help is highly appreciated. Thanks.
Here is the initialized variables of greenBitmap and redBitmap:
private static Bitmap greenBitmap = null;
private static Bitmap redBitmap = null;
private static int parentWidth = 0;
private static int parentHeight = 0;
public ViewingWindow(Context context, AttributeSet attr) {
super(context, attr);
greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
}
public ViewingWindow(Context context) {
super(context);
greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
}
The full error from DDMS:
11-21 20:53:44.959: E/AndroidRuntime(521): FATAL EXCEPTION: main
11-21 20:53:44.959: E/AndroidRuntime(521): java.lang.NullPointerException
11-21 20:53:44.959: E/AndroidRuntime(521): at com.ali.nazzal.sciencefairheartrateproject.ViewingWindow.onDraw(ViewingWindow.java:66)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.View.draw(View.java:6740)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.View.draw(View.java:6743)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-21 20:53:44.959: E/AndroidRuntime(521): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewRoot.draw(ViewRoot.java:1407)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.os.Looper.loop(Looper.java:123)
11-21 20:53:44.959: E/AndroidRuntime(521): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 20:53:44.959: E/AndroidRuntime(521): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 20:53:44.959: E/AndroidRuntime(521): at java.lang.reflect.Method.invoke(Method.java:521)
11-21 20:53:44.959: E/AndroidRuntime(521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-21 20:53:44.959: E/AndroidRuntime(521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-21 20:53:44.959: E/AndroidRuntime(521): at dalvik.system.NativeStart.main(Native Method)
----------------------------New Error from DDMS---------------------------------
11-21 21:49:44.699: E/AndroidRuntime(551): FATAL EXCEPTION: main
11-21 21:49:44.699: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ali.nazzal.sciencefairheartrateproject/com.ali.nazzal.sciencefairheartrateproject.Monitor}: android.view.InflateException: Binary XML file line #23: Error inflating class com.ali.nazzal.sciencefairheartrateproject.ViewingWindow
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 21:49:44.699: E/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 21:49:44.699: E/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
11-21 21:49:44.699: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-21 21:49:44.699: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-21 21:49:44.699: E/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
11-21 21:49:44.699: E/AndroidRuntime(551): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.ali.nazzal.sciencefairheartrateproject.ViewingWindow
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-21 21:49:44.699: E/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.Activity.setContentView(Activity.java:1647)
11-21 21:49:44.699: E/AndroidRuntime(551): at com.ali.nazzal.sciencefairheartrateproject.Monitor.onCreate(Monitor.java:60)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-21 21:49:44.699: E/AndroidRuntime(551): ... 11 more
11-21 21:49:44.699: E/AndroidRuntime(551): Caused by: java.lang.NoSuchMethodException: ViewingWindow(Context,AttributeSet)
11-21 21:49:44.699: E/AndroidRuntime(551): at java.lang.Class.getMatchingConstructor(Class.java:660)
11-21 21:49:44.699: E/AndroidRuntime(551): at java.lang.Class.getConstructor(Class.java:477)
11-21 21:49:44.699: E/AndroidRuntime(551): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-21
21:49:44.699: E/AndroidRuntime(551): ... 23 more

Add this constructor to ViewingWindow:
public ViewingWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attr, defStyle);
greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
}

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

Error on launching activity

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.

Got an error showing invalid index, size is zero on eclipse while developing for android

Got an error showing invalid index, size is zero on eclipse and failed to open on emulator.
Here is my mainactivity code and logcat error.
`package com.example.notetakingapp;
import java.util.List;
import com.example.notetakingapp.data.NoteDataSource;
import com.example.notetakingapp.data.NoteItem;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private NoteDataSource dataSource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource=new NoteDataSource(this);
List<NoteItem> notes= dataSource.findAll();
NoteItem note=notes.get(0);
note.setText("updated!!!");
dataSource.update(note);
notes=dataSource.findAll();
note=notes.get(0);
Log.i("NOTES", note.getKey() + ": " + note.getText());
}`
Logcat:
11-21 19:06:59.463: E/Trace(5216): error opening trace file: No such file or directory (2)
11-21 19:07:00.323: D/AndroidRuntime(5216): Shutting down VM
11-21 19:07:00.323: W/dalvikvm(5216): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-21 19:07:00.343: E/AndroidRuntime(5216): FATAL EXCEPTION: main
11-21 19:07:00.343: E/AndroidRuntime(5216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notetakingapp/com.example.notetakingapp.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.os.Looper.loop(Looper.java:137)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-21 19:07:00.343: E/AndroidRuntime(5216): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 19:07:00.343: E/AndroidRuntime(5216): at java.lang.reflect.Method.invoke(Method.java:511)
11-21 19:07:00.343: E/AndroidRuntime(5216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-21 19:07:00.343: E/AndroidRuntime(5216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-21 19:07:00.343: E/AndroidRuntime(5216): at dalvik.system.NativeStart.main(Native Method)
11-21 19:07:00.343: E/AndroidRuntime(5216): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-21 19:07:00.343: E/AndroidRuntime(5216): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-21 19:07:00.343: E/AndroidRuntime(5216): at java.util.ArrayList.get(ArrayList.java:304)
11-21 19:07:00.343: E/AndroidRuntime(5216): at com.example.notetakingapp.MainActivity.onCreate(MainActivity.java:23)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.Activity.performCreate(Activity.java:5104)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-21 19:07:00.343: E/AndroidRuntime(5216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-21 19:07:00.343: E/AndroidRuntime(5216): ... 11 more
11-21 19:07:03.163: I/Process(5216): Sending signal. PID: 5216 SIG: 9
If size is 0 then there are no items, so you have to check that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource=new NoteDataSource(this);
List<NoteItem> notes= dataSource.findAll();
if(notes.size() > 0)
{
NoteItem note=notes.get(0);
note.setText("updated!!!");
dataSource.update(note);
notes=dataSource.findAll();
note=notes.get(0);
Log.i("NOTES", note.getKey() + ": " + note.getText());
}
}`
datasource is initialized but not assigned. That might be the problem.
The method dataSource.findAll() is probably returning nothing.
You are trying to access the List index 0. At index 0 is the first element of you List, but if it is empty there is no element at index 0, and that's when you get an IndexOutOfBoundsException
You could add a condition to check is the List is empty or not:
if(notes.size() > 0) {
//operations
} else {
//List is empty message
}

FATAL EXCEPTION: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException

I see this error popping almost a million times and people have got the help. As a beginner, I would like to seek some help for the simple media recorder that I'm developing. Please help.
Below mentioned is the code-
add_company.java
public class add_company extends Activity{
//Adding for voice record
ImageButton play,record,stop;
private String s,voicerec;
MediaPlayer mp;
MediaRecorder mr;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add_company);
//Adding voice record
play = (ImageButton)findViewById(R.id.Play);
record = (ImageButton)findViewById(R.id.Record);
stop = (ImageButton)findViewById(R.id.Stop);
File audioFile = null;
try {
audioFile = File.createTempFile(null, ".amr");
} catch (IOException e1) {
e1.printStackTrace();
}
mr = new MediaRecorder();
s = audioFile.getAbsolutePath();
voicerec=s;
System.out.println("Path: "+s);
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mr.setOutputFile(s);
try{
mr.prepare();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void record(View v){
Toast.makeText(add_company.this, "Recording", Toast.LENGTH_LONG).show();
mr.start();
}
public void stop(View v){
mr.stop(); //this is stop recording
Toast.makeText(add_company.this, "Stopped", Toast.LENGTH_LONG).show();
mr.reset(); // this is required to avoid the error: Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)
mr.release();
}
public void play(View v){
File f = new File(voicerec);
Uri u = Uri.fromFile(f);
mp = MediaPlayer.create(add_company.this, u);
mp.setLooping(false);
mp.start();
}
}
LogCat details:
06-18 10:31:53.281: E/AndroidRuntime(2740): FATAL EXCEPTION: main
06-18 10:31:53.281: E/AndroidRuntime(2740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tg_db1/com.example.tg_db1.add_company}: java.lang.NullPointerException
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.os.Looper.loop(Looper.java:137)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 10:31:53.281: E/AndroidRuntime(2740): at dalvik.system.NativeStart.main(Native Method)
06-18 10:31:53.281: E/AndroidRuntime(2740): Caused by: java.lang.NullPointerException
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.io.File.createTempFile(File.java:999)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.io.File.createTempFile(File.java:970)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.example.tg_db1.add_company.onCreate(add_company.java:64)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.Activity.performCreate(Activity.java:5104)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-18 10:31:53.281: E/AndroidRuntime(2740): ... 11 more
File.createTempFile(null, ".amr");
Prefix can't be null.
From the source code :
public static File createTempFile(String prefix, String suffix, File directory) throws IOException
{
if (prefix == null) throw new NullPointerException();
/***/
}

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

Categories