public class BriefFragmentActivity extends Fragment
{
public BriefFragmentActivity()
{
}
public static final String ARG_SECTION_NUMBER = "section_number";
String getParameter_Url = "parameters/getParameters";
Spinner spinnerSystemType;
ArrayList<String> systemTypeArrayList;
DataBaseAdapter dataBaseAdapterInstance;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.brief_fragment_view, null);
getAllSpinnerValues();
spinnerSystemType = (Spinner) v.findViewById(R.id.spinner_systemtype);
return v;
}
private void getAllSpinnerValues()
{
systemTypeArrayList = new ArrayList<String>();
dataBaseAdapterInstance = new DataBaseAdapter(getActivity().getApplicationContext());
dataBaseAdapterInstance.openToRead();
List<Parameters> parameterList = new ArrayList<Parameters>();
parameterList = dataBaseAdapterInstance.getParameterList();
for (Parameters p : parameterList)
{
Log.i("parameter list",""+p.getId() + "cash code" + p.getCashCode() +"....." + p.getParamType());
if(p.getParamType().trim().toString().equalsIgnoreCase("system type"))
{
Log.i("system type","string comparison block");
systemTypeArrayList.add(p.getParamDescription().trim().toString());
Log.i("systemTypeArrayList:",""+systemTypeArrayList); **// getting correct data on this line from bean class.....**
//**Error is when trying to add this arrayList to ArrayAdapter**
ArrayAdapter<String> adapterSystemType = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner,systemTypeArrayList);
adapterSystemType.setDropDownViewResource(R.layout.custom_spinner_item_list);
spinnerSystemType.setAdapter(adapterSystemType);
adapterSystemType.notifyDataSetChanged();
}
else
{
Log.i("values",""+p.getParamType());
}
}
dataBaseAdapterInstance.close();
}
}
please ignore mistake of { }
i think there is some mistake of Context thats why its giving me error.Error starts from this LOC:
ArrayAdapter adapterSystemType = new ArrayAdapter(getActivity(),R.layout.custom_spinner,systemTypeArrayList);
before this everything working fine.
Here is the Logcat:
12-20 04:48:26.115: E/AndroidRuntime(20657): FATAL EXCEPTION: main
12-20 04:48:26.115: E/AndroidRuntime(20657): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.survey.management.activity/com.survey.management.activity.FragmentMainActivity}: java.lang.NullPointerException
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.os.Looper.loop(Looper.java:137)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-20 04:48:26.115: E/AndroidRuntime(20657): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 04:48:26.115: E/AndroidRuntime(20657): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 04:48:26.115: E/AndroidRuntime(20657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-20 04:48:26.115: E/AndroidRuntime(20657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-20 04:48:26.115: E/AndroidRuntime(20657): at dalvik.system.NativeStart.main(Native Method)
12-20 04:48:26.115: E/AndroidRuntime(20657): Caused by: java.lang.NullPointerException
12-20 04:48:26.115: E/AndroidRuntime(20657): at com.survey.management.activity.BriefFragmentActivity.getAllSpinnerValues(BriefFragmentActivity.java:701)
12-20 04:48:26.115: E/AndroidRuntime(20657): at com.survey.management.activity.BriefFragmentActivity.onCreateView(BriefFragmentActivity.java:67)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.Activity.performStart(Activity.java:5216)
12-20 04:48:26.115: E/AndroidRuntime(20657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
12-20 04:48:26.115: E/AndroidRuntime(20657): ... 11 more
Use
dataBaseAdapterInstance = new DataBaseAdapter(getActivity());
Also spinnerSystemType is not initialized
View v = inflater.inflate(R.layout.brief_fragment_view, null);
spinnerSystemType =(Spinner)v.findViewById(R.id.spinner1);
// make sure the id spinner1 is right
Related
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
I am trying to take the input values from EditText and I want to save it to sqlite Database. I dont know how to use the logcat [also please explain how can I read the errors from the LogCat].
MainActivity.java:
package com.example.database;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText first,last,age,classc;
Button add,view;
String FirstName;
String LastName,Class;
Integer Age;
sqLit myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new sqLit(this);
Link();
xmlToVar();
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Integer flag=myDB.insertValue(FirstName, LastName, Class, Age);
if (flag==1)
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Record Added" , Toast.LENGTH_LONG);
toast.show();
}
else
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Error Occured" , Toast.LENGTH_LONG);
toast.show();
}
}
});
}
public void Link()
{
first=(EditText) findViewById(R.id.editFirst);
last=(EditText) findViewById(R.id.editLast);
classc=(EditText) findViewById(R.id.editClass);
age=(EditText) findViewById(R.id.editAge);
add=(Button) findViewById(R.id.Add);
view=(Button) findViewById(R.id.ViewAll);
}
public void xmlToVar()
{
FirstName = first.getText().toString();
LastName = last.getText().toString();
Class = classc.getText().toString();
Age = Integer.parseInt(age.getText().toString());
}
}
sqLit.java:
package com.example.database;
import org.w3c.dom.Text;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class sqLit extends SQLiteOpenHelper
{
public static final String DB_NAME="student12.db";
public static final String TABLE_NAME="class1";
public static final String COL_1="ROLL NO";
public static final String COL_2="First Name";
public static final String COL_3="Last Name";
public static final String COL_4="Class";
public static final String COL_5="Age";
public sqLit(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table " + TABLE_NAME + "(" + COL_1 + "INTEGER AUTOINCREMENT PRIMARY KEY," + COL_2 + "TEXT," + COL_3 + "TEXT," + COL_4 + "TEXT," + COL_5 + "INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exist" + TABLE_NAME );
onCreate(db);
}
public Integer insertValue(String FirstName, String LastName, String Class, Integer Age)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentV = new ContentValues();
contentV.put(COL_2, FirstName);
contentV.put(COL_3, LastName);
contentV.put(COL_4, Class);
contentV.put(COL_5, Age);
long isInserted = db.insert(TABLE_NAME, null, contentV);
if (isInserted == -1){
return 0;
}
else
{
return 1;
}
}
}
Also, please explain how to use LogCat as I don't understand how to read it.
LOGCAT:
06-11 18:44:39.650: E/Trace(29536): error opening trace file: No such file or directory (2)
06-11 18:44:39.660: D/AndroidRuntime(29536): Shutting down VM
06-11 18:44:39.660: W/dalvikvm(29536): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:44:39.660: E/AndroidRuntime(29536): FATAL EXCEPTION: main
06-11 18:44:39.660: E/AndroidRuntime(29536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Looper.loop(Looper.java:137)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:44:39.660: E/AndroidRuntime(29536): at dalvik.system.NativeStart.main(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.onCreate(MainActivity.java:34)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:44:39.660: E/AndroidRuntime(29536): ... 11 more
06-11 18:46:03.652: D/dalvikvm(30256): Not late-enabling CheckJNI (already on)
06-11 18:46:03.682: E/Trace(30256): error opening trace file: No such file or directory (2)
06-11 18:46:03.712: D/AndroidRuntime(30256): Shutting down VM
06-11 18:46:03.712: W/dalvikvm(30256): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:46:03.712: E/AndroidRuntime(30256): FATAL EXCEPTION: main
06-11 18:46:03.712: E/AndroidRuntime(30256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Looper.loop(Looper.java:137)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:46:03.712: E/AndroidRuntime(30256): at dalvik.system.NativeStart.main(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:46:03.712: E/AndroidRuntime(30256): ... 11 more
06-11 18:53:33.378: E/Trace(2383): error opening trace file: No such file or directory (2)
06-11 18:53:33.408: D/AndroidRuntime(2383): Shutting down VM
06-11 18:53:33.408: W/dalvikvm(2383): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:53:33.408: E/AndroidRuntime(2383): FATAL EXCEPTION: main
06-11 18:53:33.408: E/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Looper.loop(Looper.java:137)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:53:33.408: E/AndroidRuntime(2383): at dalvik.system.NativeStart.main(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:53:33.408: E/AndroidRuntime(2383): ... 11 more
Caused by: java.lang.NumberFormatException: Invalid int: ""
(after FATAL EXCEPTION:) tells us you are trying to parse an int but the value you are trying to parse is an empty String.
this line (the first that references your class) tells us which line it was
at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
this is happening because you are calling it in onCreate() before the EditText has any value entered.
From the line above, we can see that the error happens in MainActivity in the method xmlToVar at line 72 which should be
Age = Integer.parseInt(age.getText().toString());
To solve this, you should call that method in an onClick or some event listener. You also should surround it in a try/catch or use some other validation checking.
You should read the following questions and their answers for more details:
What is a stack trace, and how can I use it to debug my application errors?
Unfortunately MyApp has stopped. How can I solve this?
java.lang.NumberFormatException: Invalid int: "" in android
basically, read the error it gives you then look for the first line which mentions your class. Sometimes you need to read deeper but for basic errors that is usually enough.
Also Please Explain how to use LogCat as I am 3 days Old Android Programmer. I never used Eclipse.
Note that the stacktrace isn't Eclipse or even Java/Android specific. You can get some sort of crash log like this in other languages/IDEs/Consoles
The problem is here:
Age = Integer.parseInt(age.getText().toString());
age.getText().toString() equals "" and which causes the NumberFormatException
I want to make maps and show all markers or locations in google maps. I have created a class database for latitude and longitude. I call latitude and longitude for view location in google maps. I've this code
public void onLocationChanged(Location location) {
Intent intent = new Intent(getApplicationContext(),Database_Puskesmas.class);
msg_latitude = intent.getStringExtra("dataLatitude");
msg_longitude = intent.getStringExtra("dataLogitude");
double la = Double.parseDouble(msg_latitude);
double lo = Double.parseDouble(msg_longitude);
//Database_Puskesmas db = new Database_Puskesmas(this);
LatLng latLng = new LatLng(la, lo);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
But when I run the program, I get the following error:
02-21 22:55:01.480: E/AndroidRuntime(4072): FATAL EXCEPTION: main
02-21 22:55:01.480: E/AndroidRuntime(4072): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.bpjs/com.application.bpjs.PetaPuskesmas_Activity}: java.lang.NullPointerException
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread.access$700(ActivityThread.java:134)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.os.Looper.loop(Looper.java:137)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread.main(ActivityThread.java:4867)
02-21 22:55:01.480: E/AndroidRuntime(4072): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 22:55:01.480: E/AndroidRuntime(4072): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 22:55:01.480: E/AndroidRuntime(4072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
02-21 22:55:01.480: E/AndroidRuntime(4072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
02-21 22:55:01.480: E/AndroidRuntime(4072): at dalvik.system.NativeStart.main(Native Method)
02-21 22:55:01.480: E/AndroidRuntime(4072): Caused by: java.lang.NullPointerException
02-21 22:55:01.480: E/AndroidRuntime(4072): at java.lang.StringToReal.parseDouble(StringToReal.java:244)
02-21 22:55:01.480: E/AndroidRuntime(4072): at java.lang.Double.parseDouble(Double.java:295)
02-21 22:55:01.480: E/AndroidRuntime(4072): at com.application.bpjs.PetaPuskesmas_Activity.onLocationChanged(PetaPuskesmas_Activity.java:491)
02-21 22:55:01.480: E/AndroidRuntime(4072): at com.application.bpjs.PetaPuskesmas_Activity.onCreate(PetaPuskesmas_Activity.java:151)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.Activity.performCreate(Activity.java:5047)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-21 22:55:01.480: E/AndroidRuntime(4072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
02-21 22:55:01.480: E/AndroidRuntime(4072): ... 11 more
02-21 22:55:10.409: I/Process(4072): Sending signal. PID: 4072 SIG: 9
Here:
Intent intent = new Intent(getApplicationContext(),Database_Puskesmas.class);
msg_latitude = intent.getStringExtra("dataLatitude");
msg_longitude = intent.getStringExtra("dataLogitude");
Just preparing intent without adding dataLatitude and dataLogitude key and tying to get both keys from Intent.
Use onLocationChanged method location parameter for getting Latitude and Logitude like:
double la = location.getLatitude();
double lo = location.getLongitude();
I have been working on an app to use a phone's/tablet's camera flash as a flashlight. Everything seemed to be working fine but when I tested it on my Droid Bionic running Android 4.1.2, the app failed to turn on the flash even though it said it did. Here is the java code I used:
package com.example.flash;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isFlashOn = false;
private Camera camera;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(getApplicationContext(),
"Your device doesn't have camera!",Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
}
#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
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}}
Is this code correct or did I miss something?
Logcat:
07-03 18:48:29.064: E/Trace(773): error opening trace file: No such file or directory (2)
07-03 18:48:30.535: D/Camera(773): app passed NULL surface
07-03 18:48:31.023: D/libEGL(773): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:48:31.073: D/(773): HostConnection::get() New Host Connection established 0x2a13c3c0, tid 773
07-03 18:48:31.123: D/libEGL(773): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:48:31.173: D/libEGL(773): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:48:31.406: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:48:31.433: D/OpenGLRenderer(773): Enabling debug mode 0
07-03 18:48:31.723: I/Choreographer(773): Skipped 58 frames! The application may be doing too much work on its main thread.
07-03 18:49:05.923: D/dalvikvm(773): GC_CONCURRENT freed 202K, 12% free 2623K/2956K, paused 74ms+25ms, total 234ms
07-03 18:49:06.216: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:09.584: D/Camera(773): app passed NULL surface
07-03 18:49:09.853: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:11.813: I/info(773): torch is turned on!
07-03 18:49:13.467: I/info(773): torch is turned off!
07-03 18:49:16.263: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:16.713: D/AndroidRuntime(773): Shutting down VM
07-03 18:49:16.713: W/dalvikvm(773): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:49:16.936: E/AndroidRuntime(773): FATAL EXCEPTION: main
07-03 18:49:16.936: E/AndroidRuntime(773): java.lang.RuntimeException: Method called after release()
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera._stopPreview(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera.stopPreview(Camera.java:543)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.example.flash.MainActivity.surfaceDestroyed(MainActivity.java:140)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.updateWindow(SurfaceView.java:553)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:231)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.View.dispatchWindowVisibilityChanged(View.java:7544)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1211)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Looper.loop(Looper.java:137)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:49:16.936: E/AndroidRuntime(773): at dalvik.system.NativeStart.main(Native Method)
07-03 18:49:24.854: E/Trace(811): error opening trace file: No such file or directory (2)
07-03 18:49:25.413: D/libEGL(811): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:49:25.567: D/(811): HostConnection::get() New Host Connection established 0x2a15f570, tid 811
07-03 18:49:25.643: D/libEGL(811): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:49:25.663: D/libEGL(811): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:49:25.934: W/EGL_emulation(811): eglSurfaceAttrib not implemented
07-03 18:49:25.963: D/OpenGLRenderer(811): Enabling debug mode 0
07-03 18:53:12.298: D/Camera(811): app passed NULL surface
07-03 18:53:12.723: D/dalvikvm(811): GC_CONCURRENT freed 172K, 11% free 2600K/2904K, paused 9ms+165ms, total 421ms
07-03 18:53:12.934: E/EGL_emulation(811): rcCreateWindowSurface returned 0
07-03 18:53:12.934: E/EGL_emulation(811): tid 811: eglCreateWindowSurface(631): error 0x3003 (EGL_BAD_ALLOC)
07-03 18:53:12.943: D/AndroidRuntime(811): Shutting down VM
07-03 18:53:12.943: W/dalvikvm(811): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:53:13.033: E/AndroidRuntime(811): FATAL EXCEPTION: main
07-03 18:53:13.033: E/AndroidRuntime(811): java.lang.RuntimeException: createWindowSurface failed EGL_BAD_ALLOC
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createSurface(HardwareRenderer.java:1064)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createEglSurface(HardwareRenderer.java:961)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:787)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1502)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:53:13.033: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
UPDATE
I think the key is you are running Android 4.1.2. Since Android 4.0, if you want to use the Camera Device, even if you only want to use the flash, you are forced to use a SurfaceView.
In the previous answer (below), I gave you a link to a Torch app which uses SurfaceView. Try it or adapt it to your code.
PREVIOUS ANSWER:
As stated in many other cases (like this one), you may be facing a Device-Specific issue that is quite common in the Android world.
Although getSupportedFlashModes() may return FLASH_MODE_TORCH on nearly every device, many of them don't actually support it.
Anyway, you could try these:
Use camera.startPreview(); after camera = Camera.open();
Try setting FLASH_MODE_OFF initially (before camera.startPreview();).
Check if this Torch app works in your device. In case it does, you have the source code to compare it to yours.
Download a Torch app from the Play Store to test if it's a device issue or not.
Post the issue in a Droid Bionic support forum.
UPDATE: I would say the final keyword is a problem in your code. Try changing it to:
//camera = Camera.open();
//final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
cam.stopPreview();
cam.release();
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
user the permission "android.permission.FLASHLIGHT" in the manifest
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
I am getting a NullPointerException when trying to declare the Geocoder in my application. I have the following declaration :
public class MainActivity extends Activity {
private Geocoder geocoder = new Geocoder(this, Locale.getDefault());
...
}
I get the following LogCat :
03-20 10:48:55.729: D/AndroidRuntime(604): Shutting down VM
03-20 10:48:55.729: W/dalvikvm(604): threadid=1: thread exiting with uncaught exception
(group=0x40a71930)
03-20 10:48:56.209: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-20 10:48:56.209: E/AndroidRuntime(604): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.coord/com.example.coord.MainActivity}: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:137)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 10:48:56.209: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): Caused by: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.example.coord.MainActivity.<init>(MainActivity.java:21)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.Class.newInstanceImpl(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.Class.newInstance(Class.java:1319)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
Line 21 is my Geocoder declaration. What is wrong with my code?
The context is only available when the activity is started so you cannot initialize the geocoder in the class body. Try to initialize it in the onCreate or onResume method instead...
public class MainActivity extends Activity {
private Geocoder mGeocoder;
#Override
protected void onCreate(Bundle _icicle) {
super.onCreate(_icicle);
mGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
}
}
Add this permissions in to manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and use getApplicationContext() insted of this
It is recommended to use the GeoCode in Background or on separate thread as defined by Google Developers page.
new Thread(new Runnable()
{
#Override
public void run()
{
//Do things.
Geocoder geocoder = new Geocoder(getBaseContext());
try {
// Getting a maximum of 5 Address that matches the input text
addresses = geocoder.getFromLocationName(addressText,5);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();