displaying a Prepopulated database as a listview [duplicate] - java

This question already has an answer here:
Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'"
(1 answer)
Closed 8 years ago.
I am trying to display multiple columns of a database inside a listview. i am using sqliteasset helper and a row.xml and main.xml. my mainactivity extends a listactivity. The error i'm getting is that the findviewbyid can't find a linear layout with "R.id.list" but my activity_main.xml has it.
this is my main class that displays the database info on the list.
public class MainActivity extends ListActivity {
private Cursor schedule;
private MyDatabase db;
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new MyDatabase(this);
schedule = db.getSchedule();
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new SimpleCursorAdapter(
this, // The context
R.layout.row, // The layout of the row to show
db.getSchedule(), // Here the cursor where the data is
// but it can be anything
new String[] {"fName", "fType" }, // here the tables to show
new int[] { android.R.id.text1, android.R.id.text2 }, // here where to show? the IDs
// of the layout elements where put data.
0));
}
#Override
protected void onDestroy() {
super.onDestroy();
schedule.close();
db.close();
}
}
Row:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#0000FF"
android:textSize="25dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#0000FF"
android:textSize="25dp" />
</LinearLayout>
Activity_Main: (you can see the id.list)
<?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"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
LogCat:
12-02 01:04:44.436: E/AndroidRuntime(1183): FATAL EXCEPTION: main
12-02 01:04:44.436: E/AndroidRuntime(1183): Process: com.example.mealplan, PID: 1183
12-02 01:04:44.436: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.os.Looper.loop(Looper.java:136)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-02 01:04:44.436: E/AndroidRuntime(1183): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 01:04:44.436: E/AndroidRuntime(1183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 01:04:44.436: E/AndroidRuntime(1183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 01:04:44.436: E/AndroidRuntime(1183): at dalvik.system.NativeStart.main(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-02 01:04:44.436: E/AndroidRuntime(1183): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.Activity.setContentView(Activity.java:1929)
12-02 01:04:44.436: E/AndroidRuntime(1183): at com.example.mealplan.MainActivity.onCreate(MainActivity.java:35)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.Activity.performCreate(Activity.java:5231)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-02 01:04:44.436: E/AndroidRuntime(1183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

Change this line :
android:id="#+id/list"
to
android:id="#id/android:list"

You are trying to fetch list id from android default library. You should retrieve list Id from your own layout.
Change This line
ListView listView = (ListView) findViewById(android.R.id.list);
to
ListView listView = (ListView) findViewById(R.id.list);

Related

Android app crash on startup

I really don't know whats wrong with this...
The app should only change the text of the text view when clicking a button, in this case the start button.
When I start the app with the virtual machine it crashes.
My code
package com.example.projectiii;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button btnS = (Button) findViewById(R.id.btStart);
btnS.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
TextView word = (TextView) findViewById(R.id.display);
word.setText("Project");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
My 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.projectiii.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/display"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text"
android:layout_below="#+id/text"
android:layout_marginTop="42dp"
android:text="Start" />
<Button
android:id="#+id/btFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btStart"
android:layout_alignRight="#+id/text"
android:text="Finish" />
<Chronometer
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/text"
android:layout_marginTop="40dp"
android:text="Chronometer" />
<TextView
android:id="#+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Text"
android:textSize="30sp" />
My logcat
05-03 15:06:48.720: E/AndroidRuntime(921): FATAL EXCEPTION: main
05-03 15:06:48.720: E/AndroidRuntime(921): Process: com.example.projectiii, PID: 921
05-03 15:06:48.720: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectiii/com.example.projectiii.MainActivity}: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Looper.loop(Looper.java:136)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 15:06:48.720: E/AndroidRuntime(921): at dalvik.system.NativeStart.main(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921): at com.example.projectiii.MainActivity.onCreate(MainActivity.java:33)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Activity.performCreate(Activity.java:5231)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-03 15:06:48.720: E/AndroidRuntime(921): ... 11 more
05-03 15:06:48.760: W/ActivityManager(385): Force finishing activity com.example.projectiii/.MainActivity
Make sure the xml you posted is activity_main, not fragment_main. You don't need a Fragment here, so remove all Fragment-related code.
Bind the view of TextView here:
setContentView(R.layout.activity_main);
TextView word = (TextView) findViewById(R.id.display);
the layout xml you have shown is of the fragment(since it doesn't contain the R.id. container). You are trying to access the button element defined in your Fragment layout in your activity which is giving you he null pointer. Try accessing the button from the fragment class which will resolve your error.

my android app error unfortunately app has stopped

I am new to Android.
I've just started to build my first Android app and have been struggling with this errorfor the past 2 days.
Finding no solution I had to come here.
Error:
Unfortunately "app name" has stopped
This is main activity layout:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
This is another layout:
<?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="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
logcat error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:137)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:01:29.772: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): Caused by: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:01:29.772: E/AndroidRuntime(1314): ... 11 more
03-25 12:01:35.622: I/Process(1314): Sending signal. PID: 1314 SIG: 9
03-25 12:11:56.522: D/AndroidRuntime(1378): Shutting down VM
03-25 12:11:56.522: W/dalvikvm(1378): threadid=1: thread exiting with uncaught exception (group=0xb3adbb90)
03-25 12:11:56.542: E/AndroidRuntime(1378): FATAL EXCEPTION: main
03-25 12:11:56.542: E/AndroidRuntime(1378): Process: com.example.listview, PID: 1378
03-25 12:11:56.542: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:137)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:11:56.542: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:11:56.542: E/AndroidRuntime(1378): ... 11 more
main activity code
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lst = (ListView)findViewById(R.id.listView1);
//ListView mainListView = null;
String[] test = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(test) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
// Set the ArrayAdapter as the ListView's adapter.
lst.setAdapter( listAdapter );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your listview is not contained in the activity's layout so when you call it it gives you a null pointer exception. you could put the listview inside the main activity's layout.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
Line 36 is:
lst.setAdapter( listAdapter );
Means that lst is probably null.
ListView lst = (ListView)findViewById(R.id.listView1); //NULL
Failed to find the view, because you load main_activity layout, which does not have a ListView with id=listView1.
Probably you need to load another layout, which it contains.
ListView lst = (ListView)findViewById(R.id.listView1); return null because you don't use the right content in setContentView(R.layout.activity_main);. You have two options:
Use a include to put a layout inside another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView
... />
<include android:layout="#layout\my_layout_listview" />
</RelativeLayout>
Or set the other layout which contains your ListView in:
setContentView(R.layout.my_layout_listview);

How to fix NullPointerException? (Android SDK, Eclipse)

I feel like this is a simple problem that I'm somehow missing but I can't for the life of me figure out what's going wrong with this code. Every time this class loads I get a NullPointerException
11-19 17:40:35.967: E/AndroidRuntime(2144): FATAL EXCEPTION: main
11-19 17:40:35.967: E/AndroidRuntime(2144): Process: john.brian.adlibs, PID: 2144
11-19 17:40:35.967: E/AndroidRuntime(2144): java.lang.RuntimeException: Unable to start activity ComponentInfo{john.brian.adlibs/john.brian.adlibs.StoryDisplay}: java.lang.NullPointerException
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.os.Looper.loop(Looper.java:137)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-19 17:40:35.967: E/AndroidRuntime(2144): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 17:40:35.967: E/AndroidRuntime(2144): at java.lang.reflect.Method.invoke(Method.java:515)
11-19 17:40:35.967: E/AndroidRuntime(2144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-19 17:40:35.967: E/AndroidRuntime(2144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-19 17:40:35.967: E/AndroidRuntime(2144): at dalvik.system.NativeStart.main(Native Method)
11-19 17:40:35.967: E/AndroidRuntime(2144): Caused by: java.lang.NullPointerException
11-19 17:40:35.967: E/AndroidRuntime(2144): at john.brian.adlibs.StoryDisplay.onCreate(StoryDisplay.java:25)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.Activity.performCreate(Activity.java:5243)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-19 17:40:35.967: E/AndroidRuntime(2144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-19 17:40:35.967: E/AndroidRuntime(2144): ... 11 more
and, after heavy Googling, I'm pretty sure everything is defined.
public class StoryDisplay extends Activity{
TextView storyView;
Button sdBackToStoryList;
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.story_display);
setVariables();
String story = getIntent().getExtras().getString("storyFinal");
storyView.setText(story);
sdBackToStoryList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent viewChooseStory = new Intent(StoryDisplay.this, ChooseStory.class);
startActivity(viewChooseStory);
}
});
}
private void setVariables() {
// TODO Auto-generated method stub
sdBackToStoryList = (Button) findViewById(R.id.bStoryDisplayBackToStoryList);
storyView = (TextView) findViewById(R.id.tvStoryDisplayStory);
scrollView = (ScrollView) findViewById(R.id.svStoryDisplay);
}
}
And the XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp" >
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-3337976893531833/3006951709"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, emulator-5554"
ads:loadAdOnCreate="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="10"
android:padding="20dp" >
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/svStoryDisplay"
android:layout_height="350dp">
<TextView
android:text="#string/app_name"
android:id="#+id/tvStoryDisplayStory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left" />
</ScrollView>
<Button
android:id="#+id/bStoryDisplayBackToStoryList"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="50dp"
android:textSize="20sp"
android:text="#string/backToStoryList"
/>
The only thing I can think of is the string that the storyView is set to, which is defined with the same button that starts the StoryDisplay in another class:
String storyFinal = new String("String goes here");
Intent teleport = new Intent(SadLibs.this, StoryDisplay.class);
teleport.putExtra("storyFinal",storyFinal);
startActivity(teleport);
Intent viewChooseStory = new Intent(SadLibs.this, StoryDisplay.class);
startActivity(viewChooseStory);
The error appears to be in the line:
storyView.setText(story);
Again, I'm sure this is an incredibly simple issue I'm just missing and I feel incredibly stupid for having this issue but any help would be greatly appreciated. Thank you!
In the code below
Intent teleport = new Intent(SadLibs.this, StoryDisplay.class);
teleport.putExtra("storyFinal",storyFinal);
startActivity(teleport);
Intent viewChooseStory = new Intent(SadLibs.this, StoryDisplay.class);
startActivity(viewChooseStory);
You add extra only once. If you don't add storyFinal extra, you will get NPE in the line
String story = getIntent().getExtras().getString("storyFinal");
String story = getIntent().getExtras().getString("storyFinal");
You need to check to see if the Intent or the Extras is NULL.
if (getIntent() != NULL) {
if (getIntent().getExtras() != NULL) {
story = getIntent().getExtras().getString("storyFinal");
}
}
Check like this :
if(storyView!=null)
{
storyView.setText(story);
}
Just implement
sdBackToStoryList = (Button) findViewById(R.id.bStoryDisplayBackToStoryList);
storyView = (TextView) findViewById(R.id.tvStoryDisplayStory);
scrollView = (ScrollView) findViewById(R.id.svStoryDisplay);
in onCreate().
Hope this helps.

Trying to Populate a ListView from MySQLite. Getting Errors

im new to android development and need help with populating my ListView.
The database method returns a List Array and i need to put "name" and "sex" in to the custom
ListView.
these are the relevant sections of code:
DatabaseHandler.Java
public List<Suspect> getAllContacts() {
List<Suspect> suspectList = new ArrayList<Suspect>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SUSPECTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Suspect suspect = new Suspect();
suspect.setID(Integer.parseInt(cursor.getString(0)));
suspect.setName(cursor.getString(1));
suspect.setSex(cursor.getString(2));
suspect.setHeight(cursor.getString(3));
suspect.setAge(cursor.getString(4));
suspect.setHair(cursor.getString(5));
suspect.setAdditional(cursor.getString(6));
suspect.setNationality(cursor.getString(7));
suspect.setType(cursor.getString(8));
// Adding suspect to list
suspectList.add(suspect);
} while (cursor.moveToNext());
}
// return suspect list
return suspectList;
}
Suspectview.java
DatabaseHandler db = new DatabaseHandler(this);
List<Suspect> lol = db.getAllContacts();
listView = (ListView) findViewById(R.id.suspectView);
List<String> stock_list = new ArrayList<String>();
stock_list.add("stock1");
stock_list.add("stock2");
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list = stock_list.toArray((your_array_list));
for(String s : your_array_list)
System.out.println(s)
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,R.layout.suspect_list,your_array_list);
listView.setAdapter(arrayAdapter);
Suspect_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Suspect id / Hidden by default -->
<TextView
android:id="#+id/suspect_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Suspect Name -->
<!-- Suspect Image -->
<ImageView
android:contentDescription="#string/name"
android:id="#+id/suspect_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="#9ed321"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:src="#drawable/iconcontactadd"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="#+id/suspect_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/suspect_image"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/suspect_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/suspect_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/suspect_name"
android:layout_alignBottom="#+id/suspect_name"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/suspect_name"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
activity_suspect_view.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" >
<include layout="#layout/header"
android:id="#+id/header"
/>
<ListView
android:id="#+id/suspectView"
android:layout_below="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"/>
</RelativeLayout>
Logcat.
05-03 15:18:18.023: D/dalvikvm(369): GC_EXTERNAL_ALLOC freed 75K, 52% free 2599K/5379K, external 1917K/2137K, paused 40ms
05-03 15:18:18.074: D/AndroidRuntime(369): Shutting down VM
05-03 15:18:18.074: W/dalvikvm(369): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-03 15:18:18.093: E/AndroidRuntime(369): FATAL EXCEPTION: main
05-03 15:18:18.093: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sherlock/com.example.sherlock.Suspectview}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.os.Looper.loop(Looper.java:123)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-03 15:18:18.093: E/AndroidRuntime(369): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:18:18.093: E/AndroidRuntime(369): at java.lang.reflect.Method.invoke(Method.java:507)
05-03 15:18:18.093: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-03 15:18:18.093: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-03 15:18:18.093: E/AndroidRuntime(369): at dalvik.system.NativeStart.main(Native Method)
05-03 15:18:18.093: E/AndroidRuntime(369): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-03 15:18:18.093: E/AndroidRuntime(369): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.Activity.setContentView(Activity.java:1657)
05-03 15:18:18.093: E/AndroidRuntime(369): at com.example.sherlock.Suspectview.onCreate(Suspectview.java:28)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 15:18:18.093: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-03 15:18:18.093: E/AndroidRuntime(369): ... 11 more
05-03 15:18:19.903: I/Process(369): Sending signal. PID: 369 SIG: 9
You're using ListActivity in a wrong way: see http://developer.android.com/guide/topics/ui/layout/listview.html#Example
You should simply extend Activity instead of ListActivity.
In your question's comments, you said you already changed it to Activity. But your LogCat-output still shows an error in ListActivity.onContentChanged(...). So?
The exception: "Your content must have a ListView whose id attribute is 'android.R.id.list'". Try changing the id of your ListView to this value.

Buttons on Layout with Webview

I cannot figure out why these 2 buttons are not working, i have a layout file inwhich is created when the user selects a certain theme in my application. With the layout it has a webview it has 2 buttons to goback and goforward.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward">
</Button>
<WebView
android:id="#+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.12" >
This is not the entire layout file, but here is the bit of stuff i am working with for the webview and the 2 buttons, now inside my main activity here is my button code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else if (Prefs.theme.equals("Theme2"))
setContentView(R.layout.main2);
else if (Prefs.theme.equals("Theme3"))
setContentView(R.layout.main3);
else setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.button1);
btnBackward=(Button) findViewById (R.id.button2);
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// more code within on create .....
// Later in the code
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.button2:
WebViewClientDemoActivity.web.goForward();
break;
}
//
I problem im having is by default it loads main.xml not main3 (which is were the 2 buttons are)
LogCat Errors
07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:123)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 16:00:34.802: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 16:00:34.802: E/AndroidRuntime(547): ... 11 more
First in your onCreate method find your buttons by ids :
public class YourActivity extends Activity implements OnClickListener {
Button btnForward;
Button btnBackward;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.btnForward);
btnBackward=(Button) findViewById (R.id.btnBackward);
//set listeners
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// your code here ....
}
#Override
public void onClick(View v ) {
switch(v.getId()) {
case R.id.btnBackward:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.btnForward:
WebViewClientDemoActivity.web.goForward();
break;
}
}
}
It should work. Make sure you don't have 2 buttons with the same #id.
It's a common issue when you multiplicate your buttons from one. (copy/paste)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward" />
try this.
then later in java code, do this
Button btnNext = (Button) findViewById(R.id.button2);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goForward();
}
});
Button btnBack = (Button) findViewById(R.id.button1);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goBack();
}
});

Categories