Hi I'm new to android and pardon me as it is my first time developing a mobile application for my school assignment. I have an error which made my app crashed whenever I try to start the app and my guess is that the retrieving is the culprit.
I am creating an activity that is supposed to display an empty list which will allow the user the input of CRUD. I do understand that the error is due to null but how do I make it so that it allows the display of an empty list? I would appreciate any help on how to go about this, thank you!
Here is the logcat:
02-11 18:41:53.803: W/dalvikvm(18334): threadid=1: thread exiting with uncaught exception (group=0x41763da0)
02-11 18:41:53.803: E/AndroidRuntime(18334): FATAL EXCEPTION: main
02-11 18:41:53.803: E/AndroidRuntime(18334): Process: com.example.sgrecipe, PID: 18334
02-11 18:41:53.803: E/AndroidRuntime(18334): java.lang.NullPointerException
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.fetchData(FavouriteFragment.java:174)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.initControls(FavouriteFragment.java:92)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.onStart(FavouriteFragment.java:50)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer.doFrame(Choreographer.java:573)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Handler.handleCallback(Handler.java:733)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Looper.loop(Looper.java:157)
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.app.ActivityThread.main(ActivityThread.java:5335)
02-11 18:41:53.803: E/AndroidRuntime(18334): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:41:53.803: E/AndroidRuntime(18334): at java.lang.reflect.Method.invoke(Method.java:515)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-11 18:41:53.803: E/AndroidRuntime(18334): at dalvik.system.NativeStart.main(Native Method)
FavouriteFragment.java:
package com.example.sgrecipe;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class FavouriteFragment extends Fragment implements OnClickListener{
// Primitive Variables
String selected_ID = "";
// Widget GUI Declare
EditText txtTitle, txtDesc, txtSalary;
Button btnAdd, btnUpdate, btnDelete;
ListView lvNotes;
// DB Objects
DBHelper helper;
SQLiteDatabase db;
// Adapter Object
SimpleCursorAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_favourite_fragment, container, false);
return rootView;
}
#Override
public void onStart() {
super.onStart();
initControls();
// Init DB Objects
helper = new DBHelper(getActivity());}
// Widget GUI Init
private void initControls(){
txtTitle = (EditText)getView().findViewById(R.id.txtTitle);
txtDesc = (EditText)getView().findViewById(R.id.txtDes);
txtSalary = (EditText)getView().findViewById(R.id.txtSalary);
lvNotes = (ListView)getView().findViewById(R.id.lvNotes);
btnAdd = (Button)getView().findViewById(R.id.btnAdd);
btnUpdate = (Button)getView().findViewById(R.id.btnUpdate);
btnDelete = (Button)getView().findViewById(R.id.btnDelete);
// Attached Listener
btnAdd.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnDelete.setOnClickListener(this);
lvNotes.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
String title, desc, salary;
// Display Selected Row of Listview into EditText widget
Cursor row = (Cursor) adapter.getItemAtPosition(position);
selected_ID = row.getString(0);
title = row.getString(1);
desc = row.getString(2);
salary = row.getString(3);
txtTitle.setText(title);
txtDesc.setText(desc);
txtSalary.setText(salary);
}
});
// Fetch Data from database
fetchData();
}
#Override
public void onClick(View v) {
// Perform CRUD Operation
if (v == btnAdd) {
// Add Record with help of ContentValues and DBHelper class object
ContentValues values = new ContentValues();
values.put(DBHelper.C_TITLE, txtTitle.getText().toString());
values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString());
values.put(DBHelper.C_SALARY, txtSalary.getText().toString());
// Call insert method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.insert(DBHelper.TABLE, null, values);
db.close();
clearFields();
Toast.makeText(this.getActivity(), "Successfully Added",
Toast.LENGTH_LONG).show();
// Fetch Data from database and display into listview
fetchData();
}
if (v == btnUpdate) {
// Update Record with help of ContentValues and DBHelper class
// object
ContentValues values = new ContentValues();
values.put(DBHelper.C_TITLE, txtTitle.getText().toString());
values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString());
values.put(DBHelper.C_SALARY, txtSalary.getText().toString());
// Call update method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.update(DBHelper.TABLE, values, DBHelper.C_ID + "=?",
new String[] { selected_ID });
db.close();
// Fetch Data from database and display into listview
fetchData();
Toast.makeText(this.getActivity(), "Record Updated Successfully",
Toast.LENGTH_LONG).show();
clearFields();
}
if (v == btnDelete) {
// Call delete method of SQLiteDatabase Class to delete record and
// close after performing task
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE, DBHelper.C_ID + "=?",
new String[] { selected_ID });
db.close();
// Fetch Data from database and display into listview
fetchData();
Toast.makeText(this.getActivity(), "Record Deleted Successfully",
Toast.LENGTH_LONG).show();
clearFields();
}
}
// Clear Fields
private void clearFields() {
txtTitle.setText("");
txtDesc.setText("");
txtSalary.setText("");
}
// Fetch Fresh data from database and display into listview
private void fetchData() {
db = helper.getReadableDatabase();
Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.row,
c,
new String[] { DBHelper.C_TITLE, DBHelper.C_SALARY,
DBHelper.C_DESCRIPTION },
new int[] { R.id.lblTitle, R.id.lblSalary, R.id.lblDescription });
lvNotes.setAdapter(adapter);
}
}
activity_favourite_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Notes"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" >
<requestFocus>
</requestFocus>
</EditText>
<EditText
android:id="#+id/txtDes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description" >
</EditText>
<EditText
android:id="#+id/txtSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Salary" >
</EditText>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" >
</Button>
<Button
android:id="#+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Record" >
</Button>
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" >
</Button>
</LinearLayout>
<ListView
android:id="#+id/lvNotes"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
I am just starting off with android development, any help would be appreciated!
Your problem is in the onStart method:
public void onStart()
{
super.onStart();
initControls();
// Init DB Objects
helper = new DBHelper(getActivity());
}
Notice that you're creating your helper object AFTER calling initControls(), which then calls fetchData().
In fetchData you have this line:
db = helper.getWritableDatabase();
This is the line that causes the null pointer exception because helper is null.
Change your onStart method like so:
public void onStart()
{
super.onStart();
// Init DB Objects
helper = new DBHelper(getActivity());
initControls();
}
This should solve your problem.
helper is not initialized.
In your onStart() you're only initializing it after calling initControls() which calls fetchData() that uses helper. Move the helper initialization before the initControls() call.
Better solution, move code line:
helper = new DBHelper(getActivity());
from onStart() to onCreate() where is right place to initiate object
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
Hi I am encountering a null pointer error when I click on the activity Links. The purpose of my feature is to call a list of links in the form of list view. Below is the logcat and as far as I understand it is coming from my onItemClickListener, but I can't seem to point out the null error.:
sitesList.setOnItemClickListener(new OnItemClickListener()
Links.java
package com.example.sgrecipe;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Links extends Activity {
private SitesAdapter mAdapter;
private ListView sitesList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("StackSites", "OnCreate()");
setContentView(R.layout.activity_main);
//Get reference to our ListView
sitesList = (ListView)findViewById(R.id.sitesList);
//Set the click listener to launch the browser when a row is clicked.
sitesList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
String url = mAdapter.getItem(pos).getLink();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
/*
* If network is available download the xml from the Internet.
* If not then try to use the local file from last time.
*/
if(isNetworkAvailable()){
Log.i("StackSites", "starting download Task");
SitesDownloadTask download = new SitesDownloadTask();
download.execute();
}else{
mAdapter = new SitesAdapter(getApplicationContext(), -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
sitesList.setAdapter(mAdapter);
}
}
//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
/*
* AsyncTask that will download the xml file for us and store it locally.
* After the download is done we'll parse the local file.
*/
private class SitesDownloadTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... arg0) {
//Download the file
try {
Downloader.DownloadFromUrl("https://dl.dropboxusercontent.com/u/5724095/XmlParseExample/stacksites.xml", openFileOutput("StackSites.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result){
//setup our Adapter and set it to the ListView.
mAdapter = new SitesAdapter(Links.this, -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
sitesList.setAdapter(mAdapter);
Log.i("StackSites", "adapter size = "+ mAdapter.getCount());
}
}
}
activity_links.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Links" >
<ListView
android:id="#+id/sitesList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
row_site.xml (for the listview)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iconImg"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="8dp" />
<TextView
android:id="#+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/iconImg"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/aboutTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/iconImg"
android:textSize="12sp"
android:layout_below="#id/nameTxt"
/>
</RelativeLayout>
Here is the logcat:
02-23 15:13:10.856: E/AndroidRuntime(8201): FATAL EXCEPTION: main
02-23 15:13:10.856: E/AndroidRuntime(8201): Process: com.example.sgrecipe, PID: 8201
02-23 15:13:10.856: E/AndroidRuntime(8201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgrecipe/com.example.sgrecipe.Links}: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.os.Looper.loop(Looper.java:157)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.main(ActivityThread.java:5335)
02-23 15:13:10.856: E/AndroidRuntime(8201): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201): at java.lang.reflect.Method.invoke(Method.java:515)
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-23 15:13:10.856: E/AndroidRuntime(8201): at dalvik.system.NativeStart.main(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201): Caused by: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.example.sgrecipe.Links.onCreate(Links.java:36)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.Activity.performCreate(Activity.java:5389)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
Any help is really appreciate, thank you!
Change following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.activity_links);
Seems like your using different layouts:
setContentView(R.layout.activity_main);
vs
activity_links.xml
I am trying to create an app that lets the user click a button and it will display a random month. I have created a string array in my strings.xml file. Bellow is my main.java, strings.xml and activity.xml. I try to run the app and it just force closes.
package com.example.datebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;
public class Main extends Activity
{
Button btn;
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn1);
final String[] months = getResources().getStringArray(R.array.Months);
final TextView tv =(TextView)findViewById(R.id.text1);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
int rand = (int) (Math.random() * 12);
tv.setText(months[rand]);
}
});
}
}
here is my activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="updateMonth"
android:text="#string/app_name" />
<TextView
android:id="#+id/text1"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="#array/Months" />
</LinearLayout>
here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DateButton</string>
<string-array name="Months">
<item>January</item>
<item>Feburary</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
</resources>
I'm still getting errors and the app still says unfortunately your app closed. The logcat is below.
09-11 16:55:17.472: W/Resources(1638): Converting to string: TypedValue{t=0x1/d=0x7f0c0000 a=-1 r=0x7f0c0000}
09-11 16:55:17.512: D/AndroidRuntime(1638): Shutting down VM
09-11 16:55:17.512: W/dalvikvm(1638): threadid=1: thread exiting with uncaught exception (group=0xb3a8fba8)
09-11 16:55:17.532: E/AndroidRuntime(1638): FATAL EXCEPTION: main
09-11 16:55:17.532: E/AndroidRuntime(1638): Process: com.example.datebutton, PID: 1638
09-11 16:55:17.532: E/AndroidRuntime(1638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datebutton/com.example.datebutton.Main}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.os.Looper.loop(Looper.java:136)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 16:55:17.532: E/AndroidRuntime(1638): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 16:55:17.532: E/AndroidRuntime(1638): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 16:55:17.532: E/AndroidRuntime(1638): at dalvik.system.NativeStart.main(Native Method)
09-11 16:55:17.532: E/AndroidRuntime(1638): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.example.datebutton.Main.onCreate(Main.java:24)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.Activity.performCreate(Activity.java:5231)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-11 16:55:17.532: E/AndroidRuntime(1638): ... 11 more
You forgot to add the line: setContentView(R.layout.activity); before trying to instantiate your button object btn.
To use layout in you Activity you have to connect them both together. Below is how your code should look like:
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity);
btn = (Button) findViewById(R.id.btn1);
final String[] months = getResources().getStringArray(R.array.Months);
final TextView tv =(TextView)findViewById(R.id.text1);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
int r, g, b;
int rand = (int) (Math.random() * 12);
tv.setText(months[rand]);
}
});
}
Please have a look at the following code
Form.java
Form.java is the Main activity, which means the Java file for the first page. Lot of code related to creating "List" and menu are removed.
package com.example.esoftcallmanager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Form extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
DatabaseConnector databaseConnector = new DatabaseHandler();
databaseConnector.createConnection();
ListView lv = (ListView)findViewById(android.R.id.list);
String arr[] = getResources().getStringArray(R.array.branch_list);
//lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
}
DataBaseConnector.java
This is the interface for the code, which perform database actions
package com.example.esoftcallmanager;
public interface DatabaseConnector
{
public void createConnection();
public void closeConnection();
public String getPhoneNumber();
}
DataBaseHandler.java
This is the one which perform the database operations
package com.example.esoftcallmanager;
import android.app.Activity;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
public class DatabaseHandler extends Activity implements DatabaseConnector
{
private SQLiteDatabase database;
private String dbPath = "data//data//com.example.esoftcallmanager.sqllite//esoftDatabase";
#Override
public void createConnection()
{
try
{
database = this.openOrCreateDatabase("esoftDatabase", MODE_PRIVATE, null);
database.execSQL("create table BranchNetwork(" +
"brID integer primary key autoincrement,"
+"city text,"
+"steetAddress text"
+"phoneNumber1 text"
+"phoneNumber2 text"
+"email text"
+");");
}
catch(SQLException sql)
{
Toast.makeText(this, sql.getMessage(), Toast.LENGTH_LONG).show();
}
}
#Override
public void closeConnection() {
// TODO Auto-generated method stub
}
#Override
public String getPhoneNumber() {
// TODO Auto-generated method stub
return null;
}
}
However, I am unable to run the code, because I am getting the following error.
02-11 20:35:59.350: D/dalvikvm(434): GC_EXTERNAL_ALLOC freed 52K, 53% free 2552K/5379K, external 1949K/2137K, paused 80ms
02-11 20:40:34.021: D/dalvikvm(1131): GC_EXTERNAL_ALLOC freed 42K, 53% free 2552K/5379K, external 1949K/2137K, paused 122ms
02-11 20:40:34.361: D/AndroidRuntime(1131): Shutting down VM
02-11 20:40:34.361: W/dalvikvm(1131): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 20:40:34.401: E/AndroidRuntime(1131): FATAL EXCEPTION: main
02-11 20:40:34.401: E/AndroidRuntime(1131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.esoftcallmanager/com.example.esoftcallmanager.Form}: java.lang.NullPointerException
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.os.Looper.loop(Looper.java:123)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 20:40:34.401: E/AndroidRuntime(1131): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 20:40:34.401: E/AndroidRuntime(1131): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 20:40:34.401: E/AndroidRuntime(1131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 20:40:34.401: E/AndroidRuntime(1131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 20:40:34.401: E/AndroidRuntime(1131): at dalvik.system.NativeStart.main(Native Method)
02-11 20:40:34.401: E/AndroidRuntime(1131): Caused by: java.lang.NullPointerException
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-11 20:40:34.401: E/AndroidRuntime(1131): at com.example.esoftcallmanager.DatabaseHandler.createConnection(DatabaseHandler.java:19)
02-11 20:40:34.401: E/AndroidRuntime(1131): at com.example.esoftcallmanager.Form.onCreate(Form.java:30)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 20:40:34.401: E/AndroidRuntime(1131): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 20:40:34.401: E/AndroidRuntime(1131): ... 11 more
02-11 20:40:37.381: I/Process(1131): Sending signal. PID: 1131 SIG: 9
Whenever I remove the database connection part from the Form.java, this works fine! So the error must be in the following code
DatabaseConnector databaseConnector = new DatabaseHandler();
databaseConnector.createConnection();
I do not want to add all the database operations in the Form.java. I need to split the work as I have done. But, how can I do that? Please help!
if DatabaseHandler is non Activity class then no need to extends Activity to it. you will need to create a parameterized constructor of DatabaseHandler to pass Activity context to it for creating database . change your DatabaseHandler class as:
public class DatabaseHandler implements DatabaseConnector
{
private SQLiteDatabase database;
....
Context context;
public DatabaseHandler(Context context){
this.context=context;
}
#Override
public void createConnection()
{
try
{
database = context.openOrCreateDatabase("esoftDatabase",
MODE_PRIVATE, null);
// your code here....
}
catch(SQLException sql)
{
Toast.makeText(context, sql.getMessage(),
Toast.LENGTH_LONG).show();
}
}
and pass Activity Context as from Form Activity :
DatabaseConnector databaseConnector =
new DatabaseHandler(Form.this);
databaseConnector.createConnection();
One Thing I saw was:
database.execSQL("create table BranchNetwork(" +
"brID integer primary key autoincrement,"
+"city text,"
+"steetAddress text"
+"phoneNumber1 text"
+"phoneNumber2 text"
+"email text"
+");");
You have to write:
database.execSQL("create table BranchNetwork(" +
"brID integer primary key autoincrement,"
+"city text,"
+"steetAddress text," //forgot comma
+"phoneNumber1 text," //forgot comma
+"phoneNumber2 text," // forgot commas
+"email text"
+");");
I donĀ“t know if this was the mistake, but try it...
Please have a look at the following code
activity_form.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Form" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_marginTop="10dp"
android:text="#string/title" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/listImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star" />
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Form.java
package com.example.callmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Form extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
ListView lv = (ListView)findViewById(android.R.id.list);
String arr[] = getResources().getStringArray(R.array.branch_list);
lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form, menu);
return true;
}
private class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int resource, int textViewResourceId,
Object[] objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View contentView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_layout, parent,false);
TextView tv = (TextView)findViewById(R.id.listText);
ImageView iv = (ImageView)findViewById(R.id.listImage);
String arr[] = getResources().getStringArray(R.array.branch_list);
tv.setText(arr[position]);
if(arr[position].equals("Anuradhapura"))
iv.setImageResource(R.drawable.star);
return view;
}
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Esoft Call Manager</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title"><b>Select a Branch To Call</b></string>
<string-array name="branch_list">
<item>Anuradhapura</item>
<item>Avissawella</item>
</string-array>
</resources>
When I run this code, I get the 'NullPointException'. I am adding the complete log file here
02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 21:04:01.733: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at com.example.esoftcallmanager.Form.onCreate(Form.java:24)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 21:04:01.733: E/AndroidRuntime(490): ... 11 more
I am adding the folder structure below if it helps
Why I am getting this error? How to solve this? Please help!
try this:
change this
ListView lv = (ListView)findViewById(android.R.id.list);
for:
ListView lv = (ListView)findViewById(R.id.list);
Normally they use android.R.id.list when class extends a ListActivity
Change:
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Unless you have an id in your XML in the format of android:id="#android:id/myID", you don't need to use android.R. Whenever you use one of your own IDs, you can simply use R.id.Whatever
The issue is right here. ListView lv = (ListView)findViewById(android.R.id.list); you reference the Id android.R.id.list
however in your layout you reference android:id="#+id/list" which creates an Id in the Resources class called list. so you should be referencing R.id.list if you debug you will find that the lv variable is null because it wasn't found because you referenced the wrong id.
If your are using your own layout you don't need any more to write android.R just remove android and reorganize your class
Try this.
I hope it will solve your NPE.
Change it
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Actually You have defined the list view in the layout activity_form.xml have name list so you need to write the R.id.list.
I managed to solve this by my self. Following edits were required.
activity_form.xml
The "id" of the ListView should be
android:id="#android:id/list"
Form.java
It should call the ListView in this way
ListView lv = (ListView) findViewById(android.R.id.list);
Inside getView(), the initialization of the TextView and ImageView should be like this
TextView tv = (TextView) view.findViewById(R.id.listText);
ImageView iv = (ImageView) view.findViewById(R.id.listImage);
Note the view.findViewById() part.
I am creating an android app drawer, but it fails when i call the activity. Seems like it's something wrong on line 39.
It looks something like a NullPointerException.
Could you please help me?
Java code:
package com.mysoftware.mysoftwareos.mobile;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class AllAppsActivity extends ListActivity {
LinearLayout appsLinearLayout;
ListView list;
Intent intent;
private ArrayList<ResolveInfo> mApplicationList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allapps_screen);
//Import views
appsLinearLayout = (LinearLayout)findViewById(R.id.appsLinearLayout);
//Set wallpaper
appsLinearLayout.setBackgroundResource(R.drawable.images);
//Load all apps
final PackageManager pm = this.getPackageManager();
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
Log.i("TAG", ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<ResolveInfo> adapter =
new ArrayAdapter<ResolveInfo>(this, android.R.layout.simple_list_item_1, list)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = super.getView(position, convertView, parent);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView).setText(text);
return convertView;
}
};
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final Intent intent = new Intent(Intent.ACTION_MAIN);
final ActivityInfo info = mApplicationList.get(position).activityInfo;
intent.setClassName(info.packageName, info.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/appsLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apps"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35dp" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="match_parent"
android:layout_height="13dp"
android:scaleType="fitXY"
android:src="#drawable/seperator" />
</LinearLayout>
</LinearLayout>
Logcat:
01-27 01:35:24.610: E/AndroidRuntime(2150): FATAL EXCEPTION: main
01-27 01:35:24.610: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mysoftware.mysoftwareos.mobile/com.mysoftware.mysoftwareos.mobile.AllAppsActivity}: java.lang.NullPointerException
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.os.Looper.loop(Looper.java:137)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread.main(ActivityThread.java:4499)
01-27 01:35:24.610: E/AndroidRuntime(2150): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 01:35:24.610: E/AndroidRuntime(2150): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 01:35:24.610: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
01-27 01:35:24.610: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-27 01:35:24.610: E/AndroidRuntime(2150): at dalvik.system.NativeStart.main(Native Method)
01-27 01:35:24.610: E/AndroidRuntime(2150): Caused by: java.lang.NullPointerException
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:440)
01-27 01:35:24.610: E/AndroidRuntime(2150): at com.mysoftware.mysoftwareos.mobile.AllAppsActivity.onCreate(AllAppsActivity.java:39)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.Activity.performCreate(Activity.java:4637)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
01-27 01:35:24.610: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-27 01:35:24.610: E/AndroidRuntime(2150): ... 11 more
From what I can see, from this:
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
intent is still null, you haven't made an Intent Object.
I'm not sure what Intent you're trying to match against, but you still need to instantiate it using the new keyword before using it.
Eg
intent = new Intent (Intent.ACTION_MAIN);
Also from that same call, you are shadowing the global variable list. Either consider taking out
final ArrayList<ResolveInfo>
so you are working with the global list or remove that global reference.
Moving on, I don't thik PackageManager.PERMISSION_GRANTED is a valid flag for this method, since this seems to be a result constant.
Lastly: mApplicationList is also not instantiated, but then you try to read from it. Now this may be set somewhere else, we don't have the whole code. This only affects the click on a List item.