2014-08-30 68 views
0

我已经通过XML解析来填充数据库,并且我很难在列表视图中输出它。有人可以帮助我知道为什么我的列表视图不工作?谢谢。ArrayAdapter上的NullPointerException

UserWorkout.java

package com.example.istronggyminstructor; 

@SuppressLint("InlinedApi") 
public class UserWorkout extends Activity { 
    PopupWindow popup; 
    View popupview; 
    Button addworkoutbut; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent i = getIntent(); 
     String userselected = i.getExtras().getString("userselect"); 
     setContentView(R.layout.activity_user_workout); 
     TextView tv1 = (TextView)findViewById(R.id.outputuser); 
     tv1.setText(userselected); 
     addworkoutbut = (Button)findViewById(R.id.addworkoutbut); 

    } 


    public void onClickAddWorkout(View v){ 
     v = new View 
       (this); 
     dataHandler dbHandler = new dataHandler(this, null, null, 1); 
     //xmlparser(); 

     addworkoutbut = (Button)findViewById(R.id.addworkoutbut); 
     final LayoutInflater inflator = (LayoutInflater) getBaseContext() 
       .getSystemService(LAYOUT_INFLATER_SERVICE); 
     popupview = inflator.inflate(R.layout.popup4, null); 
     popup = new PopupWindow(popupview, 
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     popup.showAtLocation(popupview, Gravity.CENTER, 0, -10); 
     popup.setFocusable(true); 
     popup.update(); 


     List<String> user = dbHandler.getAllWorkouts(); 
     //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       //android.R.layout.simple_list_item_activated_2, user); 
     ArrayAdapter<String> adapter = (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, user)); 
     final ListView lv = (ListView)popupview.findViewById(R.id.workoutlists); 
     lv.setAdapter(adapter); 
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

     Button cancelbtn = (Button) popupview.findViewById(R.id.closepopup4); 
     cancelbtn.setOnClickListener(new OnClickListener(){ 
      public void onClick(View arg0){ 
       popup.dismiss(); 
      } 
     }); 
    } 


    } 

activity_user_workout.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" 
    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.istronggyminstructor.UserWorkout" > 

    <Button 
     android:id="@+id/addworkoutbut" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:onClick="onClickAddWorkout" 
     android:text="@string/addworkout" /> 

    <TextView 
     android:id="@+id/workouthistory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/addworkoutbut" 
     android:layout_marginTop="41dp" 
     android:text="@string/workouthistory" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/outputuser" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/addworkoutbut" 
     android:layout_alignLeft="@+id/workouthistory" 
     android:layout_alignRight="@+id/workouthistory" 
     android:layout_alignTop="@+id/addworkoutbut" 
     android:textSize="40sp" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/workouthistory" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="21dp" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="280dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/workoutname" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#dcdcdc" 
       android:gravity="center" 
       android:padding="20dip" 
       android:text="col1" 
       android:textColor="#000000" /> 

      <TextView 
       android:id="@+id/totalscore" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#dcdcdc" 
       android:gravity="center" 
       android:padding="20dip" 
       android:text="col2" 
       android:textColor="#000000" /> 

      <TextView 
       android:id="@+id/datein" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#dcdcdc" 
       android:gravity="center" 
       android:padding="20dip" 
       android:text="col3" 
       android:textColor="#000000" /> 

      <TextView 
       android:id="@+id/timeincol2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#dcdcdc" 
       android:gravity="center" 
       android:padding="20dip" 
       android:text="col4" 
       android:textColor="#000000" /> 
     </TableRow> 
    </TableLayout> 

</RelativeLayout> 

的logcat:

05-25 03:40:47.540: E/AndroidRuntime(3406): FATAL EXCEPTION: main 
05-25 03:40:47.540: E/AndroidRuntime(3406): Process: com.example.istronggyminstructor, PID: 3406 
05-25 03:40:47.540: E/AndroidRuntime(3406): java.lang.NullPointerException 
05-25 03:40:47.540: E/AndroidRuntime(3406):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
05-25 03:40:47.540: E/AndroidRuntime(3406):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
05-25 03:40:47.540: E/AndroidRuntime(3406):  at 

回答

0

如果要实现自己的ArrayAdapter,然后检查你的getItem()执行,以确保您没有返回null。

如果不是,请检查您传递给ArrayAdapter的值,它看起来其中一个为空,并导致ArrayAdapter炸毁。

+0

我会检查调试器先生。谢谢 – 2014-08-30 21:30:18

相关问题