Android:应用程序意外停止

时间:2012-03-02 21:56:35

标签: android android-intent android-listview eclipse-indigo

我在这里发布一个logcat文件。如何解决runtimeException错误。 Logcat显示此错误:

  

03-03 03:12:30.716:E / AndroidRuntime(880):java.lang.RuntimeException:   无法开始活动   ComponentInfo {com.example.FirstProject / com.example.FirstProject.City}:   java.lang.RuntimeException:您的内容必须具有其id为的ListView   属性是'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

我称之为city.class的类的代码:

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);
    }
 }

4 个答案:

答案 0 :(得分:0)

您似乎没有为ListView分配ID。也许其他事情正在发生,但如果没有你的代码,很难说清楚。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

将此更改为

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

答案 3 :(得分:0)

你不能将id作为mylist1 使用

android:id="android:id/list"

(在你的xml中)

然后在你的java文件中使用

ListView lv=getListView();