Creating ListView from a Collection Set - java

I am currently developing a soundboard with the ability to add a sound as a favorite. I have a favorites tab and am working on adding sounds to a ListView when clicked. I did some research and came across a post suggesting to use a Set when avoiding duplicate items. In my application class I have a global variable: public Set<String> favorite_list = new TreeSet<String>();. In my ListViewActivity I have
ArrayList favorite_list = (ArrayList) appState.favorite_list;
setListAdapter(new ArrayAdapter<String>(this, R.layout.favorites, favorite_list));
Whenever I click the Favorites Tab, the app force closes. Any help is appreciated.
Thanks, Justin
Edit: LogCat
03-05 21:25:12.996: E/AndroidRuntime(5485): FATAL EXCEPTION: main
03-05 21:25:12.996: E/AndroidRuntime(5485): java.lang.RuntimeException: Unable to start activity ComponentInfo{vartanian.android.epicmealtimepro/vartanian.android.epicmealtimepro.Favorites}: java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.util.ArrayList
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.view.View.performClick(View.java:3511)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.view.View$PerformClick.run(View.java:14105)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Handler.handleCallback(Handler.java:605)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Looper.loop(Looper.java:137)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-05 21:25:12.996: E/AndroidRuntime(5485): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 21:25:12.996: E/AndroidRuntime(5485): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 21:25:12.996: E/AndroidRuntime(5485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 21:25:12.996: E/AndroidRuntime(5485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 21:25:12.996: E/AndroidRuntime(5485): at dalvik.system.NativeStart.main(Native Method)
03-05 21:25:12.996: E/AndroidRuntime(5485): Caused by: java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.util.ArrayList
03-05 21:25:12.996: E/AndroidRuntime(5485): at vartanian.android.epicmealtimepro.Favorites.onCreate(Favorites.java:27)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.Activity.performCreate(Activity.java:4465)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-05 21:25:12.996: E/AndroidRuntime(5485): ... 18 more
Application Class:
public class Data extends Application {
public Set<String> favorite_list = new TreeSet<String>();
}
ListView Class:
public class Favorites extends ListActivity {
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Data appState = ((Data)this.getApplication());
if(appState.favorite_list.isEmpty() == true){
appState.favorite_list.add("None");
}
ArrayList favorite_list = (ArrayList) appState.favorite_list;
setListAdapter(new ArrayAdapter<String>(this, R.layout.favorites, favorite_list));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}

Related

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

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

Error on launching activity

My application crashes whenever I launch it! whats wrong with it? I have created a database and I am trying to populate my listview from the database I have created what is the problem with the code and what is the fix for it?
02-19 22:02:07.502: D/AndroidRuntime(330): Shutting down VM
02-19 22:02:07.502: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:02:07.542: E/AndroidRuntime(330): FATAL EXCEPTION: main
02-19 22:02:07.542: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:02:07.542: E/AndroidRuntime(330): ... 11 more
02-19 22:10:47.354: D/AndroidRuntime(364): Shutting down VM
02-19 22:10:47.444: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:10:47.482: E/AndroidRuntime(364): FATAL EXCEPTION: main
02-19 22:10:47.482: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Looper.loop(Looper.java:123)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.NativeStart.main(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:10:47.482: E/AndroidRuntime(364): ... 11 more
02-19 22:11:10.302: I/Process(364): Sending signal. PID: 364 SIG: 9
02-19 22:11:17.712: D/AndroidRuntime(413): Shutting down VM
02-19 22:11:17.712: W/dalvikvm(413): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:11:17.852: E/AndroidRuntime(413): FATAL EXCEPTION: main
02-19 22:11:17.852: E/AndroidRuntime(413): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:11:17.852: E/AndroidRuntime(413): ... 11 more
02-19 22:11:19.852: I/Process(413): Sending signal. PID: 413 SIG: 9
Activity:
package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity {
private DBHelper mySQLiteAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("A for Apply");
mySQLiteAdapter.insert("B for Boy");
mySQLiteAdapter.insert("C for Cat");
mySQLiteAdapter.insert("D for Dog");
mySQLiteAdapter.insert("E for Egg");
mySQLiteAdapter.insert("F for Fish");
mySQLiteAdapter.insert("G for Girl");
mySQLiteAdapter.insert("H for Hand");
mySQLiteAdapter.insert("I for Ice-scream");
mySQLiteAdapter.insert("J for Jet");
mySQLiteAdapter.insert("K for Kite");
mySQLiteAdapter.insert("L for Lamp");
mySQLiteAdapter.insert("M for Man");
mySQLiteAdapter.insert("N for Nose");
mySQLiteAdapter.insert("O for Orange");
mySQLiteAdapter.insert("P for Pen");
mySQLiteAdapter.insert("Q for Queen");
mySQLiteAdapter.insert("R for Rain");
mySQLiteAdapter.insert("S for Sugar");
mySQLiteAdapter.insert("T for Tree");
mySQLiteAdapter.insert("U for Umbrella");
mySQLiteAdapter.insert("V for Van");
mySQLiteAdapter.insert("W for Water");
mySQLiteAdapter.insert("X for X'mas");
mySQLiteAdapter.insert("Y for Yellow");
mySQLiteAdapter.insert("Z for Zoo");
mySQLiteAdapter.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{DBHelper.KEY_CONTENT};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Database Class
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public DBHelper(Context c){
context = c;
}
public DBHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DBHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
you are Tryin to load MainActivity
java.lang.ClassNotFoundException: com.example.database.MainActivity
inside your Manifest.xml change MainActivity to AndroidSQLite.
And you must use an Asynctask for the Database operations, to avoid ANR Dialog.

using startActivityForResult

i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.

can not reuse method android OOP

In my application I can not reuse method I have declared in ArabicUtility class. My intension is to use Arabicutility class to arrange Arabic text. Therefore, what I want is passe string to the method I declared in Arabicutility class and do the conversion.
I think this is basically some problem of my knoweledge of OOP. so help me to correct this.
Here is the method I add to Arabicutility class
public void addTranslate(int rid, TextView txt1) {
String textv = getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
// Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/DroidNaskhBold.ttf");
// txt1.setTypeface(typeFace);
}
I can not declare this method as static since getResources() is a non static.I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);
but When I run the programe it crashes when I go to above activities.
here is the log cat
12-29 10:02:32.561: E/AndroidRuntime(951): FATAL EXCEPTION: main
12-29 10:02:32.561: E/AndroidRuntime(951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxx/com.xxxx.xxx.ShowMessageActivity}: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 10:02:32.561: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): Caused by: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ArabicUtilities.addTranslate(ArabicUtilities.java:252)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ShowMessageActivity.onCreate(ShowMessageActivity.java:184)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Activity.performCreate(Activity.java:5008)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-29 10:02:32.561: E/AndroidRuntime(951): ... 11 more
No need to declare addTranslate as static to get Resources in non Activity class you just need to pass Current Activity Context by using non activity class constructor or passing as in method as :
public void addTranslate(int rid, TextView txt1,Context context) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
Now you can call addTranslate from Activity class as:
arbic.addTranslate(R.string.butt18title1, txt1,Your_Current_Activity.this);
arbic.addTranslate(R.string.butt18desc1, txt2,Your_Current_Activity.this);
The LogCat shows that Context in ArabicUtility is invalid. Try using the TextView's Context instead:
public void addTranslate(int rid, TextView txt1) {
String textv = txt1.getContext().getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
If ArabicUtility is not the active Activity then you shouldn't extend Activity, you should try something like this:
public class ArabicUtility {
private Context context;
public ArabicUtility(Context context) {
this.context = context;
}
...
public void addTranslate(int rid, TextView txt1) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
}
In your current Activity use:
arbic = new ArabicUtility(this);
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);

App is force closing when i run it? Unable to start activity

When i run my app, and i press on a button it force closes. I fixed my Android manifest and i cannot find the problem. Here is the Logcat:
03-07 23:48:53.035: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed 43K, 49% free 2797K/5379K, external 1596K/2108K, paused 99ms
03-07 23:48:53.132: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 35ms
03-07 23:48:53.691: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 53ms
03-07 23:48:54.281: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 48ms
03-07 23:48:55.996: D/AndroidRuntime(5457): Shutting down VM
03-07 23:48:55.996: W/dalvikvm(5457): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:48:56.039: E/AndroidRuntime(5457): FATAL EXCEPTION: main
03-07 23:48:56.039: E/AndroidRuntime(5457): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:48:56.039: E/AndroidRuntime(5457): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.view.View.performClick(View.java:2538)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Looper.loop(Looper.java:130)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:48:56.039: E/AndroidRuntime(5457): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:48:56.039: E/AndroidRuntime(5457): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:48:56.039: E/AndroidRuntime(5457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:48:56.039: E/AndroidRuntime(5457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:48:56.039: E/AndroidRuntime(5457): at dalvik.system.NativeStart.main(Native Method)
03-07 23:50:31.398: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed 47K, 49% free 2797K/5379K, external 1596K/2108K, paused 21ms
03-07 23:50:31.457: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 22ms
03-07 23:50:31.660: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 25ms
03-07 23:50:31.753: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 20ms
03-07 23:50:41.910: D/AndroidRuntime(5553): Shutting down VM
03-07 23:50:41.910: W/dalvikvm(5553): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:50:41.933: E/AndroidRuntime(5553): FATAL EXCEPTION: main
03-07 23:50:41.933: E/AndroidRuntime(5553): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:50:41.933: E/AndroidRuntime(5553): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.view.View.performClick(View.java:2538)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Looper.loop(Looper.java:130)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:50:41.933: E/AndroidRuntime(5553): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:50:41.933: E/AndroidRuntime(5553): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:50:41.933: E/AndroidRuntime(5553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:50:41.933: E/AndroidRuntime(5553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:50:41.933: E/AndroidRuntime(5553): at dalvik.system.NativeStart.main(Native Method)
03-07 23:51:20.371: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed 48K, 49% free 2797K/5379K, external 1596K/2108K, paused 97ms
03-07 23:51:20.503: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 57ms
03-07 23:51:20.792: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 24ms
03-07 23:51:21.039: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 65ms
03-07 23:51:26.679: D/AndroidRuntime(5609): Shutting down VM
03-07 23:51:26.679: W/dalvikvm(5609): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:51:26.714: E/AndroidRuntime(5609): FATAL EXCEPTION: main
03-07 23:51:26.714: E/AndroidRuntime(5609): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:51:26.714: E/AndroidRuntime(5609): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.view.View.performClick(View.java:2538)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Looper.loop(Looper.java:130)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:51:26.714: E/AndroidRuntime(5609): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:51:26.714: E/AndroidRuntime(5609): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:51:26.714: E/AndroidRuntime(5609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:51:26.714: E/AndroidRuntime(5609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:51:26.714: E/AndroidRuntime(5609): at dalvik.system.NativeStart.main(Native Method)
03-07 23:59:24.920: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 51K, 49% free 2797K/5379K, external 1596K/2108K, paused 54ms
03-07 23:59:25.104: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 46ms
03-07 23:59:25.537: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 24ms
03-07 23:59:25.846: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 38ms
03-07 23:59:41.502: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 10K, 48% free 2828K/5379K, external 11785K/11958K, paused 25ms
03-07 23:59:41.740: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2829K/5379K, external 13363K/14068K, paused 25ms
03-07 23:59:41.865: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2830K/5379K, external 16457K/17091K, paused 20ms
03-07 23:59:42.607: D/AndroidRuntime(5784): Shutting down VM
03-07 23:59:42.607: W/dalvikvm(5784): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:59:42.783: E/AndroidRuntime(5784): FATAL EXCEPTION: main
03-07 23:59:42.783: E/AndroidRuntime(5784): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.os.Looper.loop(Looper.java:130)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:59:42.783: E/AndroidRuntime(5784): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:59:42.783: E/AndroidRuntime(5784): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:59:42.783: E/AndroidRuntime(5784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:59:42.783: E/AndroidRuntime(5784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:59:42.783: E/AndroidRuntime(5784): at dalvik.system.NativeStart.main(Native Method)
03-07 23:59:42.783: E/AndroidRuntime(5784): Caused by: java.lang.NullPointerException
03-07 23:59:42.783: E/AndroidRuntime(5784): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-07 23:59:42.783: E/AndroidRuntime(5784): at izzy.n.main1.onCreate(main1.java:52)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-07 23:59:42.783: E/AndroidRuntime(5784): ... 11 more
03-07 23:59:52.850: I/Process(5784): Sending signal. PID: 5784 SIG: 9
03-08 00:11:06.982: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 46K, 49% free 2797K/5379K, external 1596K/2108K, paused 29ms
03-08 00:11:07.033: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 20ms
03-08 00:11:07.248: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 21ms
03-08 00:11:07.350: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 19ms
03-08 00:11:10.588: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 9K, 48% free 2828K/5379K, external 11785K/11958K, paused 100ms
03-08 00:11:11.068: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 1K, 48% free 2829K/5379K, external 13363K/14068K, paused 49ms
03-08 00:11:11.365: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2830K/5379K, external 16457K/17091K, paused 47ms
03-08 00:11:11.736: D/AndroidRuntime(6029): Shutting down VM
03-08 00:11:11.736: W/dalvikvm(6029): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-08 00:11:11.752: E/AndroidRuntime(6029): FATAL EXCEPTION: main
03-08 00:11:11.752: E/AndroidRuntime(6029): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.os.Looper.loop(Looper.java:130)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 00:11:11.752: E/AndroidRuntime(6029): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:11:11.752: E/AndroidRuntime(6029): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 00:11:11.752: E/AndroidRuntime(6029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 00:11:11.752: E/AndroidRuntime(6029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 00:11:11.752: E/AndroidRuntime(6029): at dalvik.system.NativeStart.main(Native Method)
03-08 00:11:11.752: E/AndroidRuntime(6029): Caused by: java.lang.NullPointerException
03-08 00:11:11.752: E/AndroidRuntime(6029): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:11:11.752: E/AndroidRuntime(6029): at izzy.n.main1.onCreate(main1.java:52)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-08 00:11:11.752: E/AndroidRuntime(6029): ... 11 more
03-08 00:16:11.924: I/Process(6029): Sending signal. PID: 6029 SIG: 9
03-08 00:16:14.525: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed 10K, 48% free 2828K/5379K, external 11785K/11958K, paused 20ms
03-08 00:16:14.678: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed 1K, 48% free 2829K/5379K, external 13363K/14068K, paused 22ms
03-08 00:16:14.779: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed <1K, 48% free 2829K/5379K, external 16457K/17091K, paused 21ms
03-08 00:16:15.037: D/AndroidRuntime(6173): Shutting down VM
03-08 00:16:15.037: W/dalvikvm(6173): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-08 00:16:15.037: E/AndroidRuntime(6173): FATAL EXCEPTION: main
03-08 00:16:15.037: E/AndroidRuntime(6173): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.os.Looper.loop(Looper.java:130)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 00:16:15.037: E/AndroidRuntime(6173): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:16:15.037: E/AndroidRuntime(6173): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 00:16:15.037: E/AndroidRuntime(6173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 00:16:15.037: E/AndroidRuntime(6173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 00:16:15.037: E/AndroidRuntime(6173): at dalvik.system.NativeStart.main(Native Method)
03-08 00:16:15.037: E/AndroidRuntime(6173): Caused by: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.onCreate(main1.java:52)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-08 00:16:15.037: E/AndroidRuntime(6173): ... 11 more
Here is the code for Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="izzy.n"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="izzy.n.IzzynActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="izzy.n.notes"
android:label="#string/notes"></activity>
<activity
android:name="izzy.n.calculator"
android:label="#string/calculator"></activity>
<activity android:name="izzy.n.main1"
android:label="#string/app_name"></activity>
</application>
</manifest>
and finally here is the main1.java:
class MyCalendar {
public String name;
public String id;
public MyCalendar(String _name, String _id) {
name = _name;
id = _id;
}
#Override
public String toString() {
return name;
}
}
public class main1 extends Activity {
/*********************************************************************
* UI part*/
private Spinner m_spinner_calender;
private Button m_button_add;
private Button m_button_add2;
private Button m_button_getEvents;
private TextView m_text_event;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*get calendar list and populate the view*/
getCalendars();
populateCalendarSpinner();
populateAddBtn();
populateAddBtn2();
populateTextEvent();
populateGetEventsBtn();
}
private void populateCalendarSpinner() {
m_spinner_calender = (Spinner)this.findViewById(R.id.spinner_calendar);
ArrayAdapter l_arrayAdapter = new ArrayAdapter(this.getApplicationContext(), android.R.layout.simple_spinner_item, m_calendars);
l_arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_spinner_calender.setAdapter(l_arrayAdapter);
m_spinner_calender.setSelection(0);
m_spinner_calender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p_parent, View p_view,
int p_pos, long p_id) {
m_selectedCalendarId = m_calendars[(int)p_id].id;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
private void populateAddBtn() {
m_button_add = (Button) this.findViewById(R.id.button_add);
m_button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEvent();
}
});
}
private void populateAddBtn2() {
m_button_add2 = (Button) this.findViewById(R.id.button_add2);
m_button_add2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEvent2();
}
});
}
private void populateGetEventsBtn() {
m_button_getEvents = (Button) findViewById(R.id.button_get_events);
m_button_getEvents.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getLastThreeEvents();
}
});
}
private void populateTextEvent() {
m_text_event = (TextView) findViewById(R.id.text_event);
String l_str = "title: roman10 calendar tutorial test\n" +
"description: This is a simple test for calendar api\n" +
"eventLocation: #home\n" +
"start time:" + getDateTimeStr(0) + "\n" +
"end time: " + getDateTimeStr(30) + "\n" +
"event status: confirmed\n" +
"all day: no\n" +
"has alarm: yes\n";
m_text_event.setText(l_str);
}
/****************************************************************
* Data part
*/
/*retrieve a list of available calendars*/
private MyCalendar m_calendars[];
private String m_selectedCalendarId = "0";
private void getCalendars() {
String[] l_projection = new String[]{"_id", "displayName"};
Uri l_calendars;
if (Build.VERSION.SDK_INT >= 8) {
l_calendars = Uri.parse("content://com.android.calendar/calendars");
} else {
l_calendars = Uri.parse("content://calendar/calendars");
}
Cursor l_managedCursor = this.managedQuery(l_calendars, l_projection, null, null, null); //all calendars
//Cursor l_managedCursor = this.managedQuery(l_calendars, l_projection, "selected=1", null, null); //active calendars
if (l_managedCursor.moveToFirst()) {
m_calendars = new MyCalendar[l_managedCursor.getCount()];
String l_calName;
String l_calId;
int l_cnt = 0;
int l_nameCol = l_managedCursor.getColumnIndex(l_projection[1]);
int l_idCol = l_managedCursor.getColumnIndex(l_projection[0]);
do {
l_calName = l_managedCursor.getString(l_nameCol);
l_calId = l_managedCursor.getString(l_idCol);
m_calendars[l_cnt] = new MyCalendar(l_calName, l_calId);
++l_cnt;
} while (l_managedCursor.moveToNext());
}
}
/*add an event to calendar*/
private void addEvent() {
ContentValues l_event = new ContentValues();
l_event.put("calendar_id", m_selectedCalendarId);
l_event.put("title", "roman10 calendar tutorial test");
l_event.put("description", "This is a simple test for calendar api");
l_event.put("eventLocation", "#home");
l_event.put("dtstart", System.currentTimeMillis());
l_event.put("dtend", System.currentTimeMillis() + 1800*1000);
l_event.put("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
l_event.put("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
l_event.put("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
l_event.put("transparency", 0);
//0~ false; 1~ true
l_event.put("hasAlarm", 1);
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = this.getContentResolver().insert(l_eventUri, l_event);
Log.v("++++++test", l_uri.toString());
}
/*add an event through intent, this doesn't require any permission
* just send intent to android calendar
* http://www.openintents.org/en/uris*/
private void addEvent2() {
Intent l_intent = new Intent(Intent.ACTION_EDIT);
l_intent.setType("vnd.android.cursor.item/event");
//l_intent.putExtra("calendar_id", m_selectedCalendarId); //this doesn't work
l_intent.putExtra("title", "roman10 calendar tutorial test");
l_intent.putExtra("description", "This is a simple test for calendar api");
l_intent.putExtra("eventLocation", "#home");
l_intent.putExtra("beginTime", System.currentTimeMillis());
l_intent.putExtra("endTime", System.currentTimeMillis() + 1800*1000);
l_intent.putExtra("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
l_intent.putExtra("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
l_intent.putExtra("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
l_intent.putExtra("transparency", 0);
//0~ false; 1~ true
l_intent.putExtra("hasAlarm", 1);
try {
startActivity(l_intent);
} catch (Exception e) {
Toast.makeText(this.getApplicationContext(), "Sorry, no compatible calendar is found!", Toast.LENGTH_LONG).show();
}
}
/*get a list of events
* http://jimblackler.net/blog/?p=151*/
private void getLastThreeEvents() {
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
String[] l_projection = new String[]{"title", "dtstart", "dtend"};
Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, "calendar_id=" + m_selectedCalendarId, null, "dtstart DESC, dtend DESC");
//Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, null, null, null);
if (l_managedCursor.moveToFirst()) {
int l_cnt = 0;
String l_title;
String l_begin;
String l_end;
StringBuilder l_displayText = new StringBuilder();
int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
int l_colEnd = l_managedCursor.getColumnIndex(l_projection[1]);
do {
l_title = l_managedCursor.getString(l_colTitle);
l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
l_displayText.append(l_title + "\n" + l_begin + "\n" + l_end + "\n----------------\n");
++l_cnt;
} while (l_managedCursor.moveToNext() && l_cnt < 3);
m_text_event.setText(l_displayText.toString());
}
}
/************************************************
* utility part
*/
private static final String DATE_TIME_FORMAT = "yyyy MMM dd, HH:mm:ss";
public static String getDateTimeStr(int p_delay_min) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
if (p_delay_min == 0) {
return sdf.format(cal.getTime());
} else {
Date l_time = cal.getTime();
l_time.setMinutes(l_time.getMinutes() + p_delay_min);
return sdf.format(l_time);
}
}
public static String getDateTimeStr(String p_time_in_millis) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
Date l_time = new Date(Long.parseLong(p_time_in_millis));
return sdf.format(l_time);
}
}
Fix this:
03-07 23:50:41.933: E/AndroidRuntime(5553): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
ALSO
Point out which line its complaining about.
A Object in the populateCalendarSpinner() method is null . Please put try catch and debug.
Your log show
03-08 00:16:15.037: E/AndroidRuntime(6173): Caused by: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.onCreate(main1.java:52)
Activity not found is the old log. You already fixed it.
if you're sure you declared it, make sure to clean your project. Sometimes Eclipse doesn't realize your XML has changed and is acting on older versions.
<activity android:name="main1"
android:label="#string/app_name"></activity>
should be
<activity android:name="izzy.n.main1"
android:label="#string/app_name"></activity>
<activity android:name="main1"
android:label="#string/app_name"></activity>
Try with changing this to
<activity android:name="izzy.n.main1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Your log also says NullPointerException in populateCalendarSpinner(); debug and fix that too.
Just do this to debug:
try {
populateCalendarSpinner();
}
catch(NullPointerException e) {
e.printStackTrace();
}

Categories