Sectionpager title from string resources - java

I have a sectionpager in my app and want to load its title from string resources, to support different language titles.
That's my sectionpager class:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Auftrag fragmenttab1 = new Auftrag();
return fragmenttab1;
case 1:
Email fragmenttab2 = new Email();
return fragmenttab2;
case 2:
Fragen fragmenttab3 = new Fragen();
return fragmenttab3;
case 3:
Statistik fragmenttab4 = new Statistik();
return fragmenttab4;
}
return null;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return Resources.getSystem().getString(R.string.questions);
case 1:
return Resources.getSystem().getString(R.string.emails);
case 2:
return Resources.getSystem().getString(R.string.calls);
case 3:
return Resources.getSystem().getString(R.string.orders);
}
return null;
}
}
This is the error I get:
06-11 09:44:07.110: E/AndroidRuntime(18290): FATAL EXCEPTION: main
06-11 09:44:07.110: E/AndroidRuntime(18290):
android.content.res.Resources$NotFoundException: String resource ID #0x7f040024
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.content.res.Resources.getText(Resources.java:1068)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.content.res.Resources.getString(Resources.java:1162)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
com.myappp.sectionapp.SectionsPagerAdapter.getPageTitle(SectionsPagerAdapter.java:41)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.support.v4.view.PagerTitleStrip.updateText(PagerTitleStrip.java:280)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.support.v4.view.PagerTitleStrip.updateAdapter(PagerTitleStrip.java:327)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.support.v4.view.PagerTitleStrip.onAttachedToWindow(PagerTitleStrip.java:256)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.View.dispatchAttachedToWindow(View.java:13029)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2683)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2690)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2690)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2690)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2690)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2690)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1457)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.Choreographer.doCallbacks(Choreographer.java:591)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.Choreographer.doFrame(Choreographer.java:561)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.os.Handler.handleCallback(Handler.java:730)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.os.Handler.dispatchMessage(Handler.java:92)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.os.Looper.loop(Looper.java:176)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
android.app.ActivityThread.main(ActivityThread.java:5419)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
java.lang.reflect.Method.invokeNative(Native Method)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
java.lang.reflect.Method.invoke(Method.java:525)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-11 09:44:07.110: E/AndroidRuntime(18290): at
dalvik.system.NativeStart.main(Native Method)

I think it should be this:
getResources().getString(R.string.my_string);
For knowing why you are getting error please refer to this SO post.

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;

List shows as not null but ArrayAdapter throws NullPointerException

Can anyone please help me? I just don't understand why does it throw null pointer exception.
I work with ksoap2,WCF web methods and android. I checked the "list" in debugger mode and it is not null.
Here is my code:
public class MainActivity extends Activity {
String loginMemberId = "a001";
Context context;
List list;
ListView listView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getBeneficiariesNameSet BNL = new getBeneficiariesNameSet();
try {
list = BNL.execute().get();
if(list!=null && list.size()>0) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list); //null pointer exception,line 43
listView = (ListView) findViewById(R.id.listBeneficiaries);
listView.setAdapter(arrayAdapter);
}else{
Toast.makeText(context,"No data in array",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class getBeneficiariesNameSet extends AsyncTask<String, Void, List<String>> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected List<String> doInBackground(String... params) {
TestArray CS = new TestArray();
List<String> response = CS.getBeneficiariesNameSet(loginMemberId);
return response;
}
#Override
protected void onPostExecute(List<String> s) {
super.onPostExecute(s);
dialog.dismiss();
}
}
}
And here is the Log cat.
06-11 14:22:20.484 22245-22245/? W/System.err﹕ java.lang.NullPointerException
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.example.administrator.androidwcf.MainActivity$1.onClick(MainActivity.java:43)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.view.View.performClick(View.java:4443)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:18442)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5021)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
06-11 14:22:20.514 599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 112539 to 46439
06-11 14:22:20.584 599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 65759 to 46439
Your context is null. Use MainActivity.this for a context in an anon inner class of your activity.

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

using Bundle object to parse JSONobject Fragment to Fragment

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)

Nullpointer Exception at OnQueryTextListener

I want to have a SearchView on the ActionBar. But if I add a OnQueryTextListener a NullPointerException appears. Furthermore I'm unable to show the searchView in the Bar.
MainActivity.java
public class MainActivity extends ActionBarActivity implements OnQueryTextListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
#Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="de.group42.todo.MainActivity" >
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/abc_ic_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
I have no clue what I did wrong. May someone help me?
EDIT: LogCat says
06-11 23:13:10.755: E/AndroidRuntime(17723): FATAL EXCEPTION: main
06-11 23:13:10.755: E/AndroidRuntime(17723): java.lang.NullPointerException
06-11 23:13:10.755: E/AndroidRuntime(17723): at de.group42.todo.MainActivity.onCreateOptionsMenu(MainActivity.java:42)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
06-11 23:13:10.755: E/AndroidRuntime(17723): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507)
06-11 23:13:10.755: E/AndroidRuntime(17723): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934)
06-11 23:13:10.755: E/AndroidRuntime(17723): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.view.Choreographer.doFrame(Choreographer.java:560)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.os.Handler.handleCallback(Handler.java:730)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.os.Looper.loop(Looper.java:176)
06-11 23:13:10.755: E/AndroidRuntime(17723): at android.app.ActivityThread.main(ActivityThread.java:5419)
06-11 23:13:10.755: E/AndroidRuntime(17723): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 23:13:10.755: E/AndroidRuntime(17723): at java.lang.reflect.Method.invoke(Method.java:525)
06-11 23:13:10.755: E/AndroidRuntime(17723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-11 23:13:10.755: E/AndroidRuntime(17723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-11 23:13:10.755: E/AndroidRuntime(17723): at dalvik.system.NativeStart.main(Native Method)
Line 42 is adding the listener. Hope you may help me.
Thanks :)
It seem your searchView gets assigned a null
I found this question that might help you:
MenuItemCompat.getActionView always returns null
Try with changing namespace from android:actionViewClass to app:actionViewClass

Categories