【问题标题】:ListView layout problemListView 布局问题
【发布时间】:2011-04-05 18:09:23
【问题描述】:

我正在尝试预览锻炼日志,其中显示用户在一天中执行的所有锻炼以及持续时间和燃烧的卡路里,最后将显示当天燃烧的总卡路里。
当我第一次尝试这样做时,我构建了一个包含 10 行的布局,每行有 3 个文本视图,分别代表锻炼名称、持续时间和燃烧的卡路里。但这是不切实际的,因为用户条目每天都会有所不同。所以我想出了这个解决方案,我不确定它是否可以在 android 中做。
请看一下,并告诉我出了什么问题对不起,介绍太长了

布局:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TableLayout android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:stretchColumns="0">
        <TableRow android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:text="Activity" 
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content">
            </TextView>
            <TextView android:layout_width="wrap_content" 
                android:text="Duration" 
                android:layout_height="wrap_content">
            </TextView>
            <TextView android:layout_width="wrap_content" 
                android:text="Burned" 
                android:layout_height="wrap_content">
            </TextView>
        </TableRow>
        <TableRow android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        <ListView android:id="@+id/lstAct" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>
        <ListView android:id="@+id/lstDur" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>
        <ListView android:id="@+id/lstbrn" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>         
        </TableRow>
            <TableRow android:layout_width="wrap_content"
            android:layout_height="wrap_content">           
            <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Total Burned Calories">
        </TextView>
        <TextView android:id="@+id/Totbrn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Kcal">
        </TextView>
    </TableRow> 
        <Button android:id="@+id/addExcBtn" 
         android:layout_width="wrap_content" 
         android:text="Add another exercise" 
         android:layout_height="wrap_content">
        </Button>           
    </TableLayout>
</LinearLayout>

这是课程代码:

public class ExerciseDiary extends Activity implements OnClickListener {
private DataBaseHelper helper;
private ListView l1;
private ListView l2;
private ListView l3;
static String[] activity;
static Integer[] duration;
static Integer[] burned;

@Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.test);
      helper = new DataBaseHelper(this);      
      l1 = (ListView) findViewById(R.id.lstAct);
      l2 = (ListView) findViewById(R.id.lstDur);
      l3 = (ListView) findViewById(R.id.lstbrn);
      fillArray();
      l1.setAdapter(new ArrayAdapter<String>(this, R.layout.items,activity));
      l2.setAdapter(new ArrayAdapter<Integer>(this, R.layout.items,duration));
      l3.setAdapter(new ArrayAdapter<Integer>(this, R.layout.items,burned));
          View AddButton = findViewById(R.id.addExcBtn);
      AddButton.setOnClickListener(this);                 
   }
public void onClick(View v){
       switch (v.getId()) {     
            case R.id.addExcBtn:
                startActivity(new Intent(this, Exercise.class));
            break;                              
       }
   }
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK){
        startActivity(new Intent(this, Activity_DietTracker.class));
        return true;
    }
    return false;
}   
public void fillArray(){
    GregorianCalendar toDay = new GregorianCalendar();
    String today = AllCmnMtd.getDate(toDay);
    activity = new String[100];
    duration = new Integer[100];
    burned = new Integer[100];
    int i = 0;
      helper.openDataBase();          
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor c = db.rawQuery("Select * from ExerciseLog where EDate = '" + today + "'", null);
    if(c!=null){
        if(c.moveToFirst()){
            do{
                activity[i] = c.getString(c.getColumnIndex("Exercise"));
                duration[i] = c.getInt(c.getColumnIndex("Duration"));
                burned[i] = c.getInt(c.getColumnIndex("Burned"));
                i++;
            }while(c.moveToNext());
        }
    }
    helper.close();
}
}

这些是错误:

java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource (ArrayAdapter.java:351)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
at android.widget.AbsListView.obtainView(AbsListView.java:1251)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1117)
at android.widget.ListView.onMeasure(ListView.java:1030)
at android.view.View.measure(View.java:7115)
at android.widget.TableRow.measureChildBeforeLayout(TableRow.java:221)
at android.widget.LinearLayout.measureHorizontal (LinearLayout.java:619)
at android.widget.TableRow.onMeasure(TableRow.java:112)
at android.view.View.measure(View.java:7115)  

还有大约 25 个人。

如果有人知道这方面的任何信息,或者如果有其他方法可以做到这一点,请告诉我。

谢谢

【问题讨论】:

  • 我真的不知道你在那里尝试什么,但我可以告诉你你做错了。
  • 将需要一些代码围绕正在为 NullPointer 膨胀的视图。
  • 首先解释你想做什么。然后我们都会知道你想要完成什么。还要发布抛出 NullPOinter 的 ArrayAdapter 的代码

标签: android listview android-layout


【解决方案1】:

XML 只是问题的一部分。在主类中,您必须将视图的实例声明为成员数据: 私有 ListView mConversationView;

然后在你的 onCreate 中你必须初始化指针。 mConversationView = (ListView) findViewById(R.id.in);

如果你不去后者,你会得到你看到的空指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多