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);
Related
I am trying a fragment app as per a tutorial and getting following error:
06-28 04:21:47.118: D/AndroidRuntime(1491): Shutting down VM
06-28 04:21:47.128: W/dalvikvm(1491): threadid=1: thread exiting with uncaught exception (group=0xb4af7b90)
06-28 04:21:47.158: E/AndroidRuntime(1491): FATAL EXCEPTION: main
06-28 04:21:47.158: E/AndroidRuntime(1491): Process: com.example.fragmentapp, PID: 1491
06-28 04:21:47.158: E/AndroidRuntime(1491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentapp/com.example.fragmentapp.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Looper.loop(Looper.java:137)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.NativeStart.main(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.setContentView(Activity.java:1928)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.example.fragmentapp.MainActivity.onCreate(MainActivity.java:13)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.performCreate(Activity.java:5243)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 11 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.pavan.fragmentdemo.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:597)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:561)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.onCreateView(Activity.java:4777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 21 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pavan.fragmentdemo.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.fragmentapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentapp-2, /system/lib]]
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:583)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 24 more
The code is as follows:
Main Activity.java
package com.example.fragmentapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements
ListFragment.Communicator {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void Message(String OS_Name) {
DetailFragment detailfragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail_Fragment);
if (detailfragment != null && detailfragment.isInLayout()) {
detailfragment.setText(OS_Name);
}
else {
Intent intent = new Intent(getApplicationContext(),
DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(DetailActivity.os_name, OS_Name);
intent.putExtras(extras);
startActivity(intent);
}
}
}
Code of DetailActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class DetailActivity extends Activity {
public static String os_name = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.detail_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(os_name);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
detailFragment.setText(name);
}
}
}
Code of DetailFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}
// we call this method when button from listfragment is clicked
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.display_tv);
view.setText(item);
}
}
Code of ListFragment.java
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ListFragment extends Fragment implements OnClickListener {
private Communicator communicator;
Button android_btn, ios_btn, windows_btn;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator = (Communicator) activity;
}
else {
throw new
ClassCastException(activity.toString()
+ " must implemenet MyListFragment.Communicator");
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.list_fragment, container, false);
// Initialize Views android_btn = (Button)
view.findViewById(R.id.android_btn_id);
ios_btn = (Button)
view.findViewById(R.id.ios_btn_id);
windows_btn = (Button)
view.findViewById(R.id.windows_btn_id);
// set on click Listeners for buttons
android_btn.setOnClickListener(this);
ios_btn.setOnClickListener(this);
windows_btn.setOnClickListener(this);
return view;
}
//Create Interface
public interface Communicator {
public void Message(String OS_Name);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn_id:
updateFragment("Android");
break;
case R.id.ios_btn_id:
updateFragment("IOS");
break;
case R.id.windows_btn_id:
updateFragment("Windows");
break;
}
}
private void updateFragment(String OS_Name) {
communicator.Message(OS_Name);
}
}
In Layout Folder
activity_main.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="horizontal" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
<fragment
android:id="#+id/detail_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.pavan.fragmentdemo.DetailFragment" >
</fragment>
</LinearLayout>
Code of detail_fragment.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:background="#FFFF99"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40sp" />
</LinearLayout>
code of list_fragment.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:background="#CCFF99"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="#+id/android_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<Button
android:id="#+id/ios_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS" />
<Button
android:id="#+id/windows_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows" />
</LinearLayout>
In layout-port
code activity_main.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="vertical" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
</LinearLayout>
Code of detail_activity.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="vertical" >
<fragment
android:id="#+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.pavan.fragmentdemo.DetailFragment" />
</LinearLayout>
You have different names of fragments:
in xml com.pavan.fragmentdemo.MyListFragment
in java com.example.fragmentapp.MyListFragment
fix package names
I am trying to make a simple android app that will add two numbers and display the output.However, I am getting the following error continuously.
"calculator.com.process stopped unexpectedly"
I used API 10 for rendering layout and API 17 as compiler and Target SDK.
Could post the code finally after all these trouble.
This is the code of main_activity.java
package com.example.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText txtNum1;
EditText txtNum2;
TextView Display;
Button bCal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNum1=(EditText) findViewById(R.id.Num1);
txtNum1=(EditText) findViewById(R.id.Num2);
Display=(TextView)findViewById(R.id.tvDisplay);
bCal=(Button)findViewById(R.id.bAdd);
bCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Tawfiq) {
// TODO Auto-generated method stub
double x=Double.parseDouble(txtNum1.getText().toString());
double y=Double.parseDouble(txtNum2.getText().toString());
double total=x+y;
Display.setText("Sum is"+total);
}
});
}
#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);
}
}
The xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bAdd"
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.calculator.MainActivity" >
<EditText
android:id="#+id/Num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Num1"
android:layout_below="#+id/Num1"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/bAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Button" />
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Num2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Sum"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
This is the logcat.
01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130): ... 11 more
You are having problem at line no-27
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
Change the relativeLayouts Id in the xml file.
It should not be same as for the add button.
android:id="#+id/relativeLayout"
Change it as above.
Two id in the same layout cannot be same.
Your add button and the relative layout is having the same id,.
And during the findViewById(), relative layout is being caste in the Button.
That's why class cast exception is coming.
Edit:
I compiled your code:-
the next mistake you have made is-
txtNum1=(EditText) findViewById(R.id.Num2);
It should be
txtNum2=(EditText) findViewById(R.id.Num2);
Mistake-You are not assigning the reference to the second edittext.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
i am trying to develop this simple app using Android ADT and I got null point exception error. It is returning null point exception error at line 32 which is
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
My whole java code is
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.ViewGroup;
import android.os.Build;
import android.widget.*;
public class MainActivity extends ActionBarActivity {
int counter;
Button add , subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.badd1);
subtract = (Button) findViewById(R.id.bsub1);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("your total is" + counter);
}
});
}
#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;
}
}
MY xml CODE IS
<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:gravity="center_horizontal"
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.newboston.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/badd1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="34dp"
android:layout_centerHorizontal="true"
android:text="add one"
android:textSize="20dp" />
<Button
android:id="#+id/bsub1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/badd1"
android:layout_below="#+id/badd1"
android:layout_marginTop="34dp"
android:text="subtract one"
android:textSize="20dp" />
</RelativeLayout>
My logCat file is
03-25 23:10:29.219: D/AndroidRuntime(1211): Shutting down VM
03-25 23:10:29.229: W/dalvikvm(1211): threadid=1: thread exiting with uncaught exception (group=0xb2b10ba8)
03-25 23:10:29.279: E/AndroidRuntime(1211): FATAL EXCEPTION: main
03-25 23:10:29.279: E/AndroidRuntime(1211): Process: com.example.newboston, PID: 1211
03-25 23:10:29.279: E/AndroidRuntime(1211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newboston/com.example.newboston.MainActivity}: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Looper.loop(Looper.java:136)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 23:10:29.279: E/AndroidRuntime(1211): at dalvik.system.NativeStart.main(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): Caused by: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.example.newboston.MainActivity.onCreate(MainActivity.java:32)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Activity.performCreate(Activity.java:5231)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-25 23:10:29.279: E/AndroidRuntime(1211): ... 11 more
03-25 23:10:36.449: I/Process(1211): Sending signal. PID: 1211 SIG: 9
If you're using more than one layout file (e.g layout-land, layout-sw360dp), make sure all elements you reference exist on all of them.
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(...);
}
}
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.