2016-02-05 27 views
-1

所以我正在练习使用列表视图,我正在看几个问题在网上,并试图让这个有两个活动,一个与列表视图中,另外当用户将能够添加到列表中,所以这是我试图使:Android工作室应用程序不断崩溃:java.lang.RuntimeException:你的内容必须有一个ListView,其id属性是'android.R.id.list'

活动1

package com.example.nooli.listview; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 

import java.util.ArrayList; 

public class MainActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final ArrayList<String> things2 = new ArrayList<String>(); 
     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,things2); 
     getListView().setAdapter(adapter); 


     Button add = (Button)findViewById(R.id.button); 
     add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent setnew = new Intent(getApplicationContext(),Main2Activity.class); 

      } 
     }); 
     Button newb = (Button)findViewById(R.id.button2); 
     final EditText et = (EditText)findViewById(R.id.editText); 
     newb.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       things2.add(String.valueOf(et.getText())); 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,things2); 
       getListView().setAdapter(adapter); 
      } 
     }); 

    } 
} 

ACTIVITY 2

package com.example.nooli.listview; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Main2Activity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     Button b = (Button)findViewById(R.id.button2); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(),MainActivity.class); 
       startActivity(i); 
      } 
     }); 
    } 
} 

ACTIVITY 1 XML

<?xml version="1.0" encoding="utf-8"?> 
<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.nooli.listview.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:textColor="@android:color/white" 
     android:background="@drawable/plus" 
     android:text="+" 
     android:layout_alignParentBottom="true" 
     android:elevation="10dp" 
     android:layout_alignRight="@+id/list" 
     android:layout_alignEnd="@+id/list" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/list" 
     android:choiceMode="singleChoice" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

ACTIVITY 2 XML

<?xml version="1.0" encoding="utf-8"?> 
<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.nooli.listview.Main2Activity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="make new" 
     android:id="@+id/button2" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="126dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_marginTop="66dp" 
     android:width="300dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

logcat的

02-05 16:52:39.944 24684-24684/? E/Zygote: MountEmulatedStorage() 
02-05 16:52:39.944 24684-24684/? E/Zygote: v2 
02-05 16:52:39.944 24684-24684/? I/libpersona: KNOX_SDCARD checking this for 10211 
02-05 16:52:39.944 24684-24684/? I/libpersona: KNOX_SDCARD not a persona 
02-05 16:52:39.984 24684-24684/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900F_5.0 ver=27 
02-05 16:52:39.984 24684-24684/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G900F_5.0-1_0032 
02-05 16:52:39.984 24684-24684/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 
02-05 16:52:39.984 24684-24684/? I/art: Late-enabling -Xcheck:jni 
02-05 16:52:40.004 24684-24684/? D/TimaKeyStoreProvider: TimaSignature is unavailable 
02-05 16:52:40.004 24684-24684/? D/ActivityThread: Added TimaKeyStore provider 
02-05 16:52:40.054 24684-24684/com.example.nooli.listview D/ResourcesManager: creating new AssetManager and set to /data/app/com.example.nooli.listview-2/base.apk 
02-05 16:52:40.174 24684-24684/com.example.nooli.listview D/AbsListView: Get MotionRecognitionManager 
02-05 16:52:40.174 24684-24684/com.example.nooli.listview D/AndroidRuntime: Shutting down VM 
02-05 16:52:40.174 24684-24684/com.example.nooli.listview E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.nooli.listview, PID: 24684 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nooli.listview/com.example.nooli.listview.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5832) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
    at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:432) 
    at android.app.Activity.setContentView(Activity.java:2241) 
    at com.example.nooli.listview.MainActivity.onCreate(MainActivity.java:19) 
    at android.app.Activity.performCreate(Activity.java:6221) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5832) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
+0

这是一个罕见的情况,当异常消息是暗示性的,它实际上指出了正确的问题,但它似乎是你完全忽视它。 – Egor

+0

这可能不是一个解决方案,但需要注意:Botton中的android:layout_alignRight =“@ + id/list”和android:layout_alignEnd =“@ + id/list”需要更改为“@ id/list “ – Pooya

+2

[您的内容必须有一个ListView,其ID参数为'android.R.id.list'](http:// stackoverflow。com/questions/11050817/your-content-must-have-a-listview-which-id-attribute-is-android-r-id-list) – Elye

回答

0

首先,调试Android应用程序崩溃的方法是首先看崩溃的最后的“原因”。你的情况是

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 

从这里,你可以做飞机坠毁的消息一搜,即Your content must have a ListView whose id attribute is 'android.R.id.list'

如果谷歌搜索,你会发现在Your content must have a ListView whose id attribute is 'android.R.id.list'要求用最好的回答同样的问题在https://stackoverflow.com/a/11050829/3286489

因此,回答你的问题。将你的ListView的ID从

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/list" 
    android:choiceMode="singleChoice" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

转换为

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@android:id/list" 
    android:choiceMode="singleChoice" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

希望这有助于你的未来错误追捕和搜索Android解决方案。干杯!

0

最重要的线路如下:

产生的原因:了java.lang.RuntimeException:您的内容必须有一个 的ListView其id属性为 'android.R.id.list'

您的内容必须有一个ListView其id属性为“android.R.id.list”

相关问题