2016-03-28 56 views
0

这是我的主要活动ListActivity错误:“很遗憾,您的应用程序已停止”

public class MainActivity extends ListActivity { 

    private String [] mainScreenList = {"Portal","Settings","Help","About"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ArrayAdapter<String> mainScreen = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1, android.R.id.text1,mainScreenList); 
     ListView lv = (ListView) findViewById(R.id.listView); 
     lv.setAdapter(mainScreen); 
    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Object o = this.getListAdapter().getItem(position); 
     String pen = o.toString(); 
     Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show(); 
     //get selected items 
     /* if(position==0) 
     { 
      Intent i = new Intent(this, DataEntryActivity.class); 
      startActivity(i); 
     }else if(position==1) 
     { 
      Intent i = new Intent(this, DataEntryActivity.class); 
      startActivity(i); 
     }else if(position==2) 
     { 
      Intent i = new Intent(this, DataEntryActivity.class); 
      startActivity(i); 
     }else if(position==3) 
     { 
      Intent i = new Intent(this, DataEntryActivity.class); 
      startActivity(i); 
     }*/ 
    } 
} 

目前我只是想显示选择列表视图项的名称代码。

这是XML代码

<?xml version="1.0" encoding="utf-8"?> 
<ListView 
    android:id="@android:id/list" 
    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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.bernine.practicalsessions.MainActivity"> 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/listView" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 

     /> 
</ListView> 

当运行它说的应用“不幸的是,应用程序已经停止。”

+0

POST错误日志.. –

+0

需要错误日志..里面的ListView – Mohan

+1

列表视图.. ???? – Dinash

回答

0

一旦检出activity_main.xml布局文件。您在另一个ListView中包含了ListView。这是无效的。

包括ListView在任何容器如LinearLayout ,RelativeLayout , etc.而对于ListView您应该使用id作为@android:id/list。 一旦检查出此示例布局。

<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="@android:id/list" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
     </ListView> 

    </LinearLayout> 
+0

接受,如果它的工作。 :) – shobhan

+0

谢谢我按照你提到的使用线性布局来安排它,并将ID放在ListView容器中。但是,在MainActivity.java这行代码中,ListView lv =(ListView)findViewById(R.id.list); - '无法解析符号列表' – cgeekmt

+1

使用ListView lv =(ListView)findViewById(android.R.id.list); – shobhan

相关问题