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.
Related
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);
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.
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);
I'm Trying to set a different Class for Specs on my TabHost, but i had only some errors (log pasted below)
If i change my champ.setIndicator("Champion").setContent(R.id.tab1); and so on for the other tabs it works.
Could someone help me?
MY code:
TabsMain.java
package com.girardi.lolguides;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabsMain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_main);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabSpec champ = tabHost.newTabSpec("champ");
TabSpec equip = tabHost.newTabSpec("item");
champ.setIndicator("Champion").setContent(new Intent(this, Champion.class));
equip.setIndicator("Items").setContent(new Intent(this, Equip.class));
tabHost.addTab(champ);
tabHost.addTab(equip);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabs_main, menu);
return true;
}
}
Champion.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Champion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
Equip.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Equip extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Second Tab");
setContentView(textView);
}
}
activity_tabs_main.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=".TabsMain" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
LOG:
05-20 13:51:22.890: D/AndroidRuntime(22816): Shutting down VM
05-20 13:51:22.890: W/dalvikvm(22816): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:51:22.910: E/AndroidRuntime(22816): FATAL EXCEPTION: main
05-20 13:51:22.910: E/AndroidRuntime(22816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Looper.loop(Looper.java:137)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:51:22.910: E/AndroidRuntime(22816): at dalvik.system.NativeStart.main(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): Caused by: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.widget.TabHost.addTab(TabHost.java:243)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:23)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:51:22.910: E/AndroidRuntime(22816): ... 11 more
05-20 13:53:10.655: D/AndroidRuntime(24134): Shutting down VM
05-20 13:53:10.655: W/dalvikvm(24134): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:53:10.665: E/AndroidRuntime(24134): FATAL EXCEPTION: main
05-20 13:53:10.665: E/AndroidRuntime(24134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Looper.loop(Looper.java:137)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:53:10.665: E/AndroidRuntime(24134): at dalvik.system.NativeStart.main(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): Caused by: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:19)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:53:10.665: E/AndroidRuntime(24134): ... 11 more
Your activity should extend TabActivity (which is deprecated) or use Fragments (which is the right way) to use tabs. See TabActivity for more informations and examples.
EDIT: about layout you can use a relativeLayout like this:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/list"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<android.support.v4.app.TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/footerlist"
android:layout_alignParentTop="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
</RelativeLayout>
About MainActivity:
public class MainActivity extends android.support.v4.app.FragmentActivity {
private android.support.v4.app.FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(...);
}
}
I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.