2012-03-02 189 views
-1

我在这里发布logcat文件。我如何解决runtimeException错误。 logcat的是显示这个错误:从那里我打电话city.classAndroid:应用程序意外停止

03-03 03:12:30.716: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FirstProject/com.example.FirstProject.City}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' Logcat:

03-03 03:46:30.607: D/AndroidRuntime(908): Shutting down VM 
03-03 03:46:30.607: W/dalvikvm(908): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
03-03 03:46:30.657: E/AndroidRuntime(908): FATAL EXCEPTION: main 
03-03 03:46:30.657: E/AndroidRuntime(908): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FirstProject/com.example.FirstProject.City}:     java.lang.RuntimeException: Your content must have a ListView whose id attribute is  'android.R.id.list' 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-03 03:46:30.657: E/AndroidRuntime(908): at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.os.Looper.loop(Looper.java:123) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-03 03:46:30.657: E/AndroidRuntime(908): at java.lang.reflect.Method.invokeNative(Native Method) 
03-03 03:46:30.657: E/AndroidRuntime(908): at java.lang.reflect.Method.invoke(Method.java:521) 
03-03 03:46:30.657: E/AndroidRuntime(908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-03 03:46:30.657: E/AndroidRuntime(908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-03 03:46:30.657: E/AndroidRuntime(908): at dalvik.system.NativeStart.main(Native Method) 
03-03 03:46:30.657: E/AndroidRuntime(908): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ListActivity.onContentChanged(ListActivity.java:245) 
03-03 03:46:30.657: E/AndroidRuntime(908): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.Activity.setContentView(Activity.java:1647) 
03-03 03:46:30.657: E/AndroidRuntime(908): at com.example.FirstProject.City.onCreate(City.java:23) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-03 03:46:30.657: E/AndroidRuntime(908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-03 03:46:30.657: E/AndroidRuntime(908): ... 11 more 

代码类:

package com.example.FirstProject; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

//import android.widget.EditText; 
//import android.widget.RadioButton; 
//import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 

public class FirstProjectActivity extends Activity { 
/** Called when the activity is first created. */ 
protected ListAdapter adapter; 
Cursor cursor; 
protected String[] cities = {"Mumbai"}; 
ListView lv ; 
ListView stations; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    /* lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

      // When clicked, Open the Next Screen 

      if(position==0) 
      { 

      } 

      } 
    });*/ 
} 


public void myClickHandler(View view) 
{ 
    Toast.makeText(this, "button clicked",Toast.LENGTH_LONG).show(); 

    /*setContentView(R.layout.screen1); 
    ListAdapter adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, cities); 
    ListView FirstProjectActivity = (ListView) findViewById(R.id.mylist1); 
    FirstProjectActivity.setAdapter(adapter);*/ 
    Intent i=new Intent(this,City.class); 
    i.setClass(this,City.class); 
    startActivity(i); 

} 
/*public void onListItemClick(ListView parent, View view, int position, long id) { 
     // super.onListItemClick(parent, view, position, id); 
     Toast.makeText(this, "yesss",Toast.LENGTH_LONG).show(); 
     Intent intent = new Intent(this, List.class); 
     Cursor cursor = (Cursor) adapter.getItem(position); 
     intent.putExtra("tn", cursor.getInt(cursor.getColumnIndex("_id"))); 
     startActivity(intent); 
    }*/ 
} 

代码为city.class到我打电话:

package com.example.FirstProject; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class City extends ListActivity{ 

protected ListAdapter adapter; 
Cursor cursor; 
protected String[] cities = {"Mumbai"}; 

@Override 
    public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.screen1); 
    ListAdapter adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, cities); 
    ListView FirstProjectActivity = (ListView) findViewById(R.id.mylist1); 
    FirstProjectActivity.setAdapter(adapter); 

} 

public void onListItemClick(ListView parent, View view, int position, long id) { 
     super.onListItemClick(parent, view, position, id); 
     Toast.makeText(this, "yesss",Toast.LENGTH_LONG).show(); 
     Intent intent = new Intent(this, List.class); 
     Cursor cursor = (Cursor) adapter.getItem(position); 
     intent.putExtra("tn", cursor.getInt(cursor.getColumnIndex("_id"))); 
     startActivity(intent); 
    } 
} 
+0

发布相关代码。 – kosa 2012-03-02 22:02:14

+0

嘿所有的人,我已经发布的代码,请帮助解决问题。如果你想要更多的代码,如xml等,请让我知道,我会尽快发布它,给你的时间。 – TechHelper 2012-03-02 22:35:39

+0

检查我添加的答案。 – kosa 2012-03-02 22:38:30

回答

0
ListView FirstProjectActivity = (ListView) findViewById(R.id.mylist1); 

改变这

ListView FirstProjectActivity = (ListView) findViewById(R.id.list); 
+0

我所显示的列表是名称为“mylist1”,那么我如何将mylist1更改为list.i已尝试过它给错误。 – TechHelper 2012-03-03 07:28:39

0

看起来你没有为你的ListView分配一个ID。也许其他事情正在发生,但是如果没有你的代码很难说。

0

看起来你使用的是ListActivity,但忘记添加ListView(或没有设置它的ID)。 您需要添加<ListView android:id="@android:id/list" ...your properties..."/>布局

0

你不能有id作为mylist1 使用

android:id="android:id/list" 

(在你的XML)

然后在你的java文件中使用

ListView lv=getListView();