I am having a few issues running my custom CursorAdapter in my application. According to my logcat, the error I get:
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
occurs at line 71 which sets the adapter (obj.setAdapter(myAdapter)). Initially I thought my database data retrieve function (Cursor chatCursor = mydb.selectConversation(msgId);) did not return any data but after testing it with the chatCursor.getCount() function, I realized that was not the case. Kindly assist me in solving this issue. Below are the codes for my activity, adapter and logcat. Thanks in advance.
MyAdapter.java
public class MyAdapter extends CursorAdapter {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public MyAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.chat_left, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView user = (TextView) view.findViewById(R.id.reply_user);
TextView msg = (TextView) view.findViewById(R.id.reply_msg);
String theTime = cursor.getString(cursor.getColumnIndexOrThrow("message_id"));
String theMessage = cursor.getString(cursor.getColumnIndexOrThrow("message"));
user.setText(theTime);
msg.setText(String.valueOf(theMessage));
}
}
MyActivity.java
public class MyActivity extends ListActivity {
//Utils Class
Utils util;
final Context context = this;
private ProgressBar dialog;
ListView obj;
DBHelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
util = new Utils(this);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
final Integer msgId = extras.getInt("id");
obj = (ListView) findViewById(R.id.list);
Cursor chatCursor = mydb.selectConversation(msgId);
MyAdapter myAdapter = new MyAdapter(this, chatCursor);
obj.setAdapter(myAdapter);
}
}
logcat
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.name.MyActivity.onCreate(MyActivity.java:71)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
11-08 06:41:03.238 559-832/? W/ActivityManager? Force finishing activity com.app.name/.MyActivity
11-08 06:41:03.742 559-574/? W/ActivityManager? Activity pause timeout for ActivityRecord{42e71938 u0 com.app.name/.MyActivity}
Take a look to your activity_view.xml. Probably the listview ID isn't "list" as you state when you try to find it with findViewById(R.layout.list).
Related
i tried to use a library (https://android-arsenal.com/details/1/1414) for a sticky header in a list view. I this configured well the library because i can use classes from there.
The code i'm using is like in the example from the link above, but i get a nullPointerExeption at ".build();"
This is the code :
public class MainActivity extends Activity {
ViewGroup containerLayout ;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
containerLayout = (ViewGroup) findViewById(R.id.containerLayout);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[]{"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
StikkyHeaderBuilder.stickTo(listView)
.setHeader(R.id.header,containerLayout )
.minHeightHeader(250)
.animator(new IconAnimator())
.build();
} }
And this is my error log:
08-14 15:10:57.188 32562-32562/com.example.cretucalinn.stickyheader1
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cretucalinn.stickyheader1, PID: 32562
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.cretucalinn.stickyheader1/com.example.cretucalinn.stickyheader1.MainActivity}:
java.lang.NullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at
it.carlom.stikkyheader.core.StikkyHeader.disallowTouchBehindHeader(StikkyHeader.java:47)
at
it.carlom.stikkyheader.core.StikkyHeader.build(StikkyHeader.java:38)
at
it.carlom.stikkyheader.core.StikkyHeaderBuilder$ListViewBuilder.build(StikkyHeaderBuilder.java:113)
at
it.carlom.stikkyheader.core.StikkyHeaderBuilder$ListViewBuilder.build(StikkyHeaderBuilder.java:95)
at
com.example.cretucalinn.stickyheader1.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:5458)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
I’m using Sherlock library and I want to create a tab in my app but I’m getting a NullPointerException; what am I doing wrong?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bonsitesherlock/com.example.bonsitesherlock.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
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:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TabHost.addTab(TabHost.java:229)
at com.example.bonsitesherlock.MainActivity.addTab(MainActivity.java:49)
at com.example.bonsitesherlock.MainActivity.setTabs(MainActivity.java:32)
at com.example.bonsitesherlock.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:4479)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
... 11 more
This is my MainActivity:
public class MainActivity extends SherlockActivity {
private TabHost mTabHost;
ProgressDialog pDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
setTabs();
}
private void setTabs()
{
addTab("", R.drawable.tab_news, News.class);
addTab("", R.drawable.tab_servises, News.class);
addTab("", R.drawable.tab_profile, News.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
Intent intent = new Intent(getApplicationContext(), c);
TabHost.TabSpec spec = mTabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mTabHost.addTab(spec);
}
}
This is my fragment activity i am getting error while accessing database my code is :-
public class Test extends Fragment {
View view;
private Cursor c;
private final String DB = "Canary";
private SQLiteDatabase db2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.test, container, false);
Button b = (Button) view.findViewById(R.id.test_button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Listning",Toast.LENGTH_LONG).show();
}
});
db2 = SQLiteDatabase.openOrCreateDatabase(DB, null);
Log.d("Lee", "working her1");
c=db2.rawQuery("SELECT * FROM business_master", null);
Log.d("Lee", "working here2");
c.moveToFirst();
String id=c.getString(c.getColumnIndex("id"));
Log.d("id", "" + id);
String email=c.getString(c.getColumnIndex("email"));
Log.d("email", email);
String password=c.getString(c.getColumnIndex("password"));
db2.close();
return view;
} }
The error i am getting :-
01-20 11:35:57.168 17081-17081/com.purplefront.canary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.purplefront.canary, PID: 17081
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
at com.purplefront.canary.Test.onCreateView(Test.java:36)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
If i remove the database access it will work fine as expected but i need database access in fragment only.
Any help would be appreciated.
I want to know why my application is throwing a runtime exception when I run it? I used to the code in the following link for my application,
dynamically add and remove view to viewpager.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab=getActionBar();
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById (R.id.pager);
pager.setAdapter (pagerAdapter);
inflater = (LayoutInflater)getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v0 = (FrameLayout) inflater.inflate (R.layout.listlayout, null);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
pagerAdapter.addView (v0, 0);
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
pagerAdapter.addView (v0);
pagerAdapter.notifyDataSetChanged();
}
}
LogCat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistworkable/com.example.todolistworkable.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.example.todolistworkable.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
the object on line 43 of your code within your onCreate method is null. Most likely the target of one of your findViewById calls is not returning what you expect it to.
See this for more info.
How to interpret Logcat
I want to refresh data in ListFragment from my custom adapter class, but got the error NullPointerException.
This is Adaper class method:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BasketFragment basketFragment = new BasketFragment();
basketFragment.getBsketList();
}
});
And when I call it in my Fragment class I get java.lang.NullPointerException at mDataBase = dbHelper.getWritableDatabase();
getBasketList():
public void refreshList() {
dbAdaper.setArrayMyData(selectAll());
dbAdaper.notifyDataSetChanged();
}
And here is the part where I got the error:
public ArrayList<BasketData> selectAll() {
Cursor mCursor = null;
dbHelper = new DBHelper(getActivity());
mDataBase = dbHelper.getWritableDatabase();//NullPointerException
mCursor = mDataBase.query("orderTable", null, null, null, null, null, null);
LogCat:
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.epon.fragments.BasketFragment.selectAll(BasketFragment.java:100)
at com.epon.fragments.BasketFragment.getBsketList(BasketFragment.java:142)
at com.epon.adapters.DBBasketAdapter$2.onClick(DBBasketAdapter.java:177)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
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)
Have you set the context right?
Android: NullPointerException on getWritableDatabase()
NullPointerException on getWritableDatabase()
I think that his is your problem a problem.
Maybe if this is not helping posting the content of getActivity() might be helpful?
edit
Your logcat makes me keep thinking that its a context error!