using Bundle object to parse JSONobject Fragment to Fragment - java

i am using Bundle object to parse jsonobject one fragment to another fragment.but when running App it crash..can any one help me....
Fragment 1
after getting my jsonobject using Async task using button i open the my second Fragment
public void onClick(View v) {
Log.d(TAG,"More button clicked");
Fragment_account_details fragment = new Fragment_account_details();
Bundle bundle = new Bundle();
String accountDetails = name_value_list.toString();
bundle.putString("accountDetails", accountDetails);
fragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment.newInstance(),
fragment.TAG).commit();
};
Fragment 2
public class Fragment_account_details extends Fragment {
TextView name, officePhone,fax,email;
public static Fragment_account_details newInstance() {
return new Fragment_account_details();
}
public final static String TAG = Fragment_account_details.class
.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_account_details, container,false);
name = (TextView) rootView.findViewById(R.id.text_name);
fax = (TextView) rootView.findViewById(R.id.text_fax);
email = (TextView) rootView.findViewById(R.id.text_email);
String accountDetails = getArguments().getString("accountDetails");
return rootView;
}
}
Logcat
12-09 11:18:22.291: D/Fragment_Entry_Account(1486): More button clicked
12-09 11:18:22.307: D/AndroidRuntime(1486): Shutting down VM
12-09 11:18:22.307: W/dalvikvm(1486): threadid=1: thread exiting with uncaught exception (group=0xa4cacb20)
12-09 11:18:22.311: E/AndroidRuntime(1486): FATAL EXCEPTION: main
12-09 11:18:22.311: E/AndroidRuntime(1486): Process: com.wakensys.sugercrm_wakensys, PID: 1486
12-09 11:18:22.311: E/AndroidRuntime(1486): java.lang.NullPointerException
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.wakensys.sugercrm_wakensys.fragments.Fragment_account_details.onCreateView(Fragment_account_details.java:41)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.handleCallback(Handler.java:733)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Looper.loop(Looper.java:136)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-09 11:18:22.311: E/AndroidRuntime(1486): at dalvik.system.NativeStart.main(Native Method)

Related

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

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

Java null pointer exception on using simple Intent Extra

When I try to send some text over to the next Activity the program crashes saying "null pointer exception unable to start activity" but without using Intent Extra the Intent works fine.
Main Activity
public class MainActivity extends Activity implements OnClickListener{
EditText et;
public final static String KEY="com.example.myprotrial";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,MyActivity.class);
i.putExtra(KEY, et.getText().toString());
startActivity(i);
}
}
MyActivity
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent=getIntent();
String a = intent.getStringExtra(MainActivity.KEY);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(a);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
}
LogCat
12-09 13:58:33.115: E/AndroidRuntime(1588): FATAL EXCEPTION: main
12-09 13:58:33.115: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprotrial/com.example.myprotrial.MyActivity}: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Looper.loop(Looper.java:137)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invoke(Method.java:525)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 13:58:33.115: E/AndroidRuntime(1588): at dalvik.system.NativeStart.main(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.example.myprotrial.MyActivity.onCreate(MyActivity.java:18)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Activity.performCreate(Activity.java:5133)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-09 13:58:33.115: E/AndroidRuntime(1588): ... 11 more
The problem is that your LogCat doesn't tell on which line the NPE occurs.
From your code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent=getIntent();
String a = intent.getStringExtra(MainActivity.KEY);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(a);
}
The if the NPE is occurring on intent.getStringExtra then your intent is null. If it's on tv.setText(a) then most likely the activity_my layout doesn't have textView1 in it. Step through it in the debugger and watch the instance variables.

NullPointerException not found using ListActivity and ArrayAdapter

Hi there stackoverflow ,
Now I ran into another problem ,I am working on a ListActivity from a custom ArrayAdapter so I can fully customize my Listview to include TextViews , switches , checkboxes .... I got a NullPointerException without I know from where its originating ! Here is my current code :
public class MenuActivity extends SherlockListActivity {
ListView scroll;
int time;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
scroll = this.getListView();
this.setListAdapter(new MyAdapter(this.getApplicationContext()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
}
public View find(int x){
return findViewById(x);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
switch (position){
case (0):{
Toast.makeText(getApplicationContext(), "Hello you just chose \"Splash\"", Toast.LENGTH_SHORT).show();
}break;
case (1):{
Toast.makeText(getApplicationContext(), "Its another one here !", Toast.LENGTH_SHORT).show();
}break;
case (2):{
Toast.makeText(getApplicationContext(), String.valueOf(id) +" and "+ position, Toast.LENGTH_SHORT).show();
}break;
}
}
public void showToast(String toast){
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
}
class MyAdapter extends ArrayAdapter<String>{
LayoutInflater inf = LayoutInflater.from(this.getContext());
public MyAdapter(Context context) {
super(context , 0 ,stringArray);
inf.from(context);
// TODO Auto-generated constructor stub
}
public static String[] stringArray= new String[]{"Splash" , "Another Example" , "Future Checkable"};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null){
inf.inflate(R.layout.normal_row, parent , false);
}
switch(position){
case(0):
convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);
break;
}
return convertView;
}
}
And My StackTrace :
12-09 17:52:55.631: E/AndroidRuntime(11921): FATAL EXCEPTION: main
12-09 17:52:55.631: E/AndroidRuntime(11921): java.lang.NullPointerException
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.makeAndAddView(ListView.java:1831)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.fillDown(ListView.java:674)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.fillFromTop(ListView.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.layoutChildren(ListView.java:1652)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Handler.handleCallback(Handler.java:725)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Looper.loop(Looper.java:137)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 17:52:55.631: E/AndroidRuntime(11921): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:52:55.631: E/AndroidRuntime(11921): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 17:52:55.631: E/AndroidRuntime(11921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 17:52:55.631: E/AndroidRuntime(11921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 17:52:55.631: E/AndroidRuntime(11921): at dalvik.system.NativeStart.main(Native Method)
Change this:
if (convertView == null){
inf.inflate(R.layout.normal_row, parent , false);
}
to:
if (convertView == null){
convertView = inf.inflate(R.layout.normal_row, parent , false);
}
Also, regarding having two types of rows in the ListView you should look at implementing the getViewTypeCount() and getItemViewType() methods of the adapter(as your current code will not work that well).

Categories