我的应用程序继续崩溃

时间:2013-01-18 20:04:10

标签: android

我正在使用Eclipse。我的应用程序从android中的列表视图中选择一个值,并且必须在另一个活动中显示该值,但它没有发生。

当我点击列表时,我得到错误活动已停止工作

我在所有应用程序中都遇到这样的错误

我根据本教程制作了另一个应用程序

http://www.vogella.com/articles/AndroidSQLite/article.html

我的代码包含两项活动:

MainActivity.java

package event.scheduler;
import android.os.Bundle;    
import android.util.Log;    
import android.view.View;    
import android.app.ListActivity;    
import android.content.Intent;    
import android.widget.ArrayAdapter;    
//import android.widget.Toast;    
import android.widget.ListView;    
public class MainActivity extends ListActivity {
public final static String EXTRA_MESSAGE = "event.scheduler.MESSAGE";

@Override
    protected void onCreate(Bundle savedInstanceState) {
    try
    {
    super.onCreate(savedInstanceState);
    String[] values = new String[]{"Conatus","Horizon","Orja","Phoniex"};
    ArrayAdapter <String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
    setListAdapter(adapter);
}
    catch(RuntimeException e){
        Log.i("sdv","dsvg");
        }
    }           

@Override   
protected void onListItemClick(ListView l, View v, int position, long id) {
     try
        {

    String item = (String)getListAdapter().getItem(position);
   Intent intent = new Intent(this,Display.class);
   intent.putExtra(EXTRA_MESSAGE,item);
   startActivity(intent);
   }

catch(RuntimeException e)
{
    Log.i("log","view");
}
}          // Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 

}

Display.java

     package event.scheduler;

     import android.os.Bundle;
     import android.app.Activity;
     import android.app.ListActivity;
     import android.content.Intent;
     import android.util.Log;

     import android.widget.TextView;

     public class Display extends Activity {

TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    //try
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    textview= (TextView)findViewById(R.id.your_texbox);
    textview.setTextSize(40);
    textview.setText(message);

    setContentView(textview);

    }
    //catch(RuntimeException e)
    {
    //  Log.i("sfs", "message");
        }
    }

}

我的xml文件是

activity_display.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"
tools:context=".Display" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

activity_main.xml中

               <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</ListView>

我的清单文件是

               <?xml version="1.0" encoding="utf-8"?>
               <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="event.scheduler"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="event.scheduler.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="event.scheduler.Display"
        android:label="@string/title_activity_display" >
    </activity>
</application>

我的logcat是

01-20 14:49:01.171: D/AndroidRuntime(1278): Shutting down VM
01-20 14:49:01.181: W/dalvikvm(1278): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-20 14:49:01.301: E/AndroidRuntime(1278): FATAL EXCEPTION: main
01-20 14:49:01.301: E/AndroidRuntime(1278): java.lang.RuntimeException: Unable to start activity ComponentInfo{event.scheduler/event.scheduler.Display}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.os.Looper.loop(Looper.java:137)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at dalvik.system.NativeStart.main(Native Method)
01-20 14:49:01.301: E/AndroidRuntime(1278): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.view.ViewGroup.addView(ViewGroup.java:3186)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:289)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.Activity.setContentView(Activity.java:1901)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at event.scheduler.Display.onCreate(Display.java:26)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.Activity.performCreate(Activity.java:5104)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-20 14:49:01.301: E/AndroidRuntime(1278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-20 14:49:01.301: E/AndroidRuntime(1278):     ... 11 more

1 个答案:

答案 0 :(得分:2)

在日志中:

  

您的内容必须包含其属性为的ListView   'android.R.id.list'

如果要扩展ListActivity

,请将ListView ID更改为android:id="@android:id/list"
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</ListView>
相关问题