Android SQLite Exception - java

I'm using a SQLite database to store some data. When retrieving I get an exception. I have no clue what i'm doing wrong. That's why i'm asking this question.
Database code:
//Creating the database
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+garbageTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colName+ " TEXT, "+colInterval+ " INTEGER, " +colDate+ " INTEGER, " +colImage+ " INTEGER)");
}
//Insert some data
public void insertGarbageObject(GarbageObject g){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(colName, g.name);
cv.put(colInterval, g.dayInterval);
cv.put(colDate, g.date.getTimeInMillis());
cv.put(colImage, g.image);
db.insert(garbageTable, colID, cv);
db.close();
}
//Read all inserted data
public ArrayList<GarbageObject> getGarbageObjects(){
ArrayList<GarbageObject> garbageObjects = new ArrayList<GarbageObject>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cur=db.rawQuery("SELECT * FROM "+garbageTable,new String [] {});
for(int i = 0; i<cur.getCount(); i++){
GarbageObject g = new GarbageObject(c.getString(R.string.naam));
g.ID = cur.getInt(cur.getColumnIndexOrThrow(colID));//Exception occurs!
g.name = cur.getString(cur.getColumnIndexOrThrow(colName));//Exception occurs!
g.dayInterval = cur.getInt(cur.getColumnIndexOrThrow(colInterval));//Exception occurs!
g.date.setTimeInMillis(cur.getLong(cur.getColumnIndexOrThrow(colDate)));//Exception occurs!
g.image = cur.getInt(cur.getColumnIndexOrThrow(colImage));//Exception occurs!
garbageObjects.add(g);
}
cur.close();
db.close();
return garbageObjects;
}
My logcat:
10-14 17:35:58.356: W/dalvikvm(18570): threadid=1: thread exiting with uncaught exception (group=0x40f27680)
10-14 17:35:58.356: E/AndroidRuntime(18570): FATAL EXCEPTION: main
10-14 17:35:58.356: E/AndroidRuntime(18570): java.lang.RuntimeException: Unable to start activity ComponentInfo{robin.garbagereminder/robin.activities.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.ActivityThread.access$600(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.os.Handler.dispatchMessage(Handler.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.os.Looper.loop(Looper.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.ActivityThread.main(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 17:35:58.356: E/AndroidRuntime(18570): at java.lang.reflect.Method.invoke(Method.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at dalvik.system.NativeStart.main(Native Method)
10-14 17:35:58.356: E/AndroidRuntime(18570): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.database.AbstractCursor.checkPosition(AbstractCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at robin.garbagereminder.DatabaseHelper.getGarbageObjects(DatabaseHelper.java:59)
10-14 17:35:58.356: E/AndroidRuntime(18570): at robin.activities.MainActivity.init(MainActivity.java:88)
10-14 17:35:58.356: E/AndroidRuntime(18570): at robin.activities.MainActivity.onCreate(MainActivity.java:33)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.Activity.performCreate(Activity.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
10-14 17:35:58.356: E/AndroidRuntime(18570): ... 12 more

You don't use the SQliteCursor correctly, it works this way:
if (cur != null ) {
if (cur.moveToFirst()) {
do {
String tmp = c.getString(R.string.naam)
...
}while (cur.moveToNext());
}
}
In your Example you do nothing with the Cursor Object, and also nothing with the Cursor. Adding cur.moveToNext() at the beginning of the for-Loop will be exactly the same, but cleaner version is the while-loop.

Related

Failed to Fill Spinner Values From Bean Class using Java in Android?

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

Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.

implements OnScrollListener inside listFragment

I implemented OnScrollListener interface in ListFragment, and i want to change text when last element of list is visible but it doesn't work. I didn't find example of similar problem(OnScrollListener inside ListFragment). My example:
public class MyListFragment1 extends ListFragment implements OnScrollListener {
public View view;
public class MyListAdapter extends ArrayAdapter<String> {
Context myContext;
List<String> lista;
public MyListAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
myContext = context;
lista = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
LayoutInflater inflater =
(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.month);
label.setText(lista.get(position));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
//Customize your icon here
icon.setImageResource(R.drawable.ic_launcher);
return row;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
ListAdapter myListAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
month);
setListAdapter(myListAdapter);
*/
List<String> lista = new ArrayList<String>();
lista.add("January");
lista.add("February");
lista.add("March");
lista.add("April");
lista.add("May");
lista.add("June");
lista.add("July");
lista.add("August");
lista.add("September");
lista.add("October");
lista.add("November");
lista.add("December");
MyListAdapter myListAdapter =
new MyListAdapter(getActivity(), R.layout.row, lista);
setListAdapter(myListAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.listfragment1, container, false);
changeText("jestem tutaj");
return view;
}
private void changeText(String txt){
TextView nowy = (TextView)view.findViewById(R.id.article);
nowy.setText(txt);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(
getActivity(),
getListView().getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (++firstVisibleItem + visibleItemCount > totalItemCount) {
changeText("something");
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
I don't have any error, it just doesn't work ;)
P.S implementation of onScroll method i found in StackOverflow.
EDIT:/ adding logcat:
10-14 01:11:34.944: E/AndroidRuntime(817): FATAL EXCEPTION: main
10-14 01:11:34.944: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exercise.AndroidListFragment/com.exercise.AndroidListFragment.AndroidListFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Looper.loop(Looper.java:137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 01:11:34.944: E/AndroidRuntime(817): at dalvik.system.NativeStart.main(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.setContentView(Activity.java:1881)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.AndroidListFragmentActivity.onCreate(AndroidListFragmentActivity.java:11)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.performCreate(Activity.java:5104)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 11 more
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: java.lang.IllegalStateException: Content view not yet created
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.ensureList(ListFragment.java:386)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.getListView(ListFragment.java:280)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.MyListFragment1.onCreateView(MyListFragment1.java:93)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Fragment.performCreateView(Fragment.java:1695)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.onCreateView(Activity.java:4717)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 21 more
EDIT 2: I have by tried findViewById but it doesn't work too....
You have implemented the OnScrollListener, but you havent attached it to your ListView.
getListView().setOnScrollListener(this);
add this to your onCreateView Method.
You have this error because you are trying to access your Listview when this one is not created yet.
Caused by: java.lang.IllegalStateException: Content view not yet
created
Your Listview is actually created by the last line of your onCreateView method, at the line which usually look something like this:
return inflater.inflate(R.layout.fragment_list, container, false);
The solution is to access your list from the onActivityCreated method, which is executed just after the onCreateView method.
#Override
public void onActivityCreated (Bundle savedInstanceState){
// Always call the superclass so it can save the view hierarchy state
super.onCreate(savedInstanceState);
getListView().setOnScrollListener(this);
}

Dialog crashing when TextView set

I have the following code which is called by the onClick event of buttons contained within a ListView.
public void viewExerciseHistory(View view) {
// History button click handler
LinearLayout parentRow = (LinearLayout)view.getParent();
TextView exerciseNameTextView = (TextView)parentRow.getChildAt(0);
String exerciseName = (String)exerciseNameTextView.getText();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.exercise_history_dialog);
dialog.setTitle(exerciseName);
TextView headerTextView = (TextView)view.findViewById(R.id.exercise_history_dialog_name_textview);
//headerTextView.setText("History");
Button closeDialogButton = (Button)view.findViewById(R.id.exercise_history_dialog_close_button);
//closeDialogButton.setOnClickListener(new OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
//});
dialog.show();
}
The ListView row is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/exercise_history_dialog_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="15sp" />
<Button android:id="#+id/exercise_history_dialog_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:typeface="monospace"
android:textSize="12sp"
android:textColor="#color/button_blue_text"
android:text="#string/button_text_close"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
The above code works fine and displays the dialog, but if uncomment out the headerTextView.setText("History") or the closeDialog.setOnClickListener it crashes with a NullPointerException.
Can anyone explain what I'm doing wrong?
EDIT LogCat:
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:19.915: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.945: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:20.435: D/AndroidRuntime(26167): Shutting down VM
10-14 21:36:20.435: W/dalvikvm(26167): threadid=1: thread exiting with uncaught exception (group=0x40e27930)
10-14 21:36:20.445: E/AndroidRuntime(26167): FATAL EXCEPTION: main
10-14 21:36:20.445: E/AndroidRuntime(26167): java.lang.IllegalStateException: Could not execute method of the activity
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3599)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View.performClick(View.java:4204)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$PerformClick.run(View.java:17355)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.handleCallback(Handler.java:725)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Looper.loop(Looper.java:137)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 21:36:20.445: E/AndroidRuntime(26167): at dalvik.system.NativeStart.main(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.reflect.InvocationTargetException
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3594)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 11 more
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.NullPointerException
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.example.workoutlog.ManageWorkouts.viewExerciseHistory(ManageWorkouts.java:269)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 14 more
It's crashing because this line:
TextView headerTextView = (TextView) findViewById(R.id.exercise_history_dialog_name_textview);
is actually returning null.
The findViewById method is looking for the view on the main activity's content view. Instead you want to use the findViewById method on the view object. Calling findViewById on the view object will allow you to get the fields you are after.
dialog.setContentView(R.layout.exercise_history_dialog);
TextView headerTextView = (TextView) dialog.findViewById(R.id.exercise_history_dialog_name_textview);
headerTextView.setText("History");
Try changing these
TextView headerTextView = (TextView)findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)findViewById(R.id.exercise_history_dialog_close_button);
to
TextView headerTextView = (TextView)dialog.findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)dialog.findViewById(R.id.exercise_history_dialog_close_button);
You have to tell which view you want to findViewById from

Android Error: Unable To Start Activity

Here is the code I use to bring up the activity:
startActivity(new Intent(getApplicationContext(), Giveaway.class));
Here is the Activity that I am bringing up:
public class Giveaway extends Activity implements OnClickListener{
static SharedPreferences settings;
SharedPreferences.Editor prefEditor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.giveaway);
settings = getSharedPreferences("firsttime", 0);
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
facebook.setOnClickListener(this);
later.setOnClickListener(this);
dontshowagain.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.facebooklayout:
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
startActivity(new Intent("android.intent.action.VIEW", localuri));
break;
case R.id.later:
finish();
break;
case R.id.dontshowagain:
finish();
prefEditor = settings.edit();
prefEditor.putBoolean("showgiveaway", false);
prefEditor.commit();
break;
}
}
I have declared the Activity in my manifest folder:
<activity
android:name=".Giveaway"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:screenOrientation="portrait"/>
But I keep getting a java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException error. Here is my logcat:
07-24 12:43:59.082: E/AndroidRuntime(7039): FATAL EXCEPTION: main
07-24 12:43:59.082: E/AndroidRuntime(7039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Looper.loop(Looper.java:123)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 12:43:59.082: E/AndroidRuntime(7039): at dalvik.system.NativeStart.main(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): Caused by: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
You need to post your layout code, but what is most likely happening is that one of these three lines is returning a null value:
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
In the debugger step through those lines and if one is null, there is your problem because as soon as you try to set the on click listener it is going to fail.
Caused by: java.lang.NullPointerException
at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
Tells us that there is a NullPointerException in Giveaway.onCreate(), specifically Giveaway.java line 29. Odds are facebook, later, and/or dontshowagain is really null. Do you have all three of these defined in giveaway.xml?
Try this...
1.
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
2. This below lines points to the class and the lines which are null.
Class:
com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}: java.lang.NullPointerException
Lines:
Please check the below four lines, i think you are getting null value here.
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);

Categories