0

我在将 ListView 插入 Activity 时遇到问题。我刚刚插入了一个从 ListActivity 扩展的列表。这很好用。我也知道如何通过选择一行来转发到另一个活动。但这一次它不起作用。我只是尝试逐行调试所有内容,并始终检查来自 LogCat 的错误消息。我也拉。但现在我不知道该做什么。一切似乎都在正确的地方。

通过记录数据库中的元素(在第二个块中),我发现最后一个元素是空的。因此,我将 for 循环减少了一个。所以整个方法“initVars()”工作,直到它结束,再次返回到“onCreate”块并停止工作。我只是复制错误消息,也许有人知道这意味着什么。

public class ActivityDetail extends Activity {

    TextView tvDefinition, tvDurationSingle, tvDurationMultiplied, tvSubunits;
    ListView lvExecution;
    String executions[];
    ArrayList<Execution> allExecutionsFromDatabase;
    ArrayAdapter<String> adapter;
    UserInformation userInfo = UserInformation.getInstance();
    MyFramework myFramework;

    // Extras from intent
    String definition;
    int activityId, durationSingle, durationMultiplied, subunits;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitydetail);

    initVars();

}

private void initVars() {

    myFramework = new MyFramework();

    // load extras into variables
    definition = myFramework.getExtrasString("activitiyDefinition",
            getIntent().getExtras());
    activityId = myFramework.getExtrasInt("activityId", getIntent()
            .getExtras());
    durationSingle = myFramework.getExtrasInt("activityDuration",
            getIntent().getExtras());
    subunits = myFramework.getExtrasInt("activitySubunits", getIntent()
            .getExtras());
    durationMultiplied = durationSingle * subunits;

    // init ui elements
    tvDefinition = (TextView) findViewById(R.id.tvActDetailDefinition);
    tvDurationSingle = (TextView) findViewById(R.id.tvActDetailDurationSingle);
    tvDurationMultiplied = (TextView) findViewById(R.id.tvActDetailDurationMultiplied);
    tvSubunits = (TextView) findViewById(R.id.tvActDetailSubunits);
    lvExecution = (ListView) findViewById(R.id.lvActDetail);

    // get executions from db
    ActivityDB entries = new ActivityDB(this);
    entries.open();
    allExecutionsFromDatabase = entries.getSpecificExecutions(
            userInfo.getUserId(), activityId);
    entries.close();

    // set list array to same size as allExecutionsFromDatabase
    executions = new String[allExecutionsFromDatabase.size()];

    // insert definition of each execution in executions for listview
    for (int i = 0; i < allExecutionsFromDatabase.size() - 1; i++) {
        Execution e = new Execution();
        e = allExecutionsFromDatabase.get(i);
        executions[i] = e.getTimestamp();
        Log.e("DEFINITION", "" + e.getTimestamp());
    }

    // init array adapter
    adapter = new ArrayAdapter<String>(ActivityDetail.this,
            android.R.layout.simple_list_item_1, executions);

    // add adapter and onClickListener to listview
    lvExecution.setAdapter(adapter);

    lvExecution.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            String a = ((TextView) view).getText().toString();
            if (a == "Foo")
                Toast.makeText(getApplicationContext(), "Foobar! xD",
                        Toast.LENGTH_SHORT).show();
        }
    });

}

洛卡特

01-08 23:55:25.361: E/AndroidRuntime(23627): FATAL EXCEPTION: main
01-08 23:55:25.361: E/AndroidRuntime(23627): java.lang.NullPointerException
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at  android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.ListView.makeAndAddView(ListView.java:1745)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.ListView.fillDown(ListView.java:670)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.ListView.fillFromTop(ListView.java:727)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.ListView.layoutChildren(ListView.java:1598)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.AbsListView.onLayout(AbsListView.java:1260)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.View.layout(View.java:7175)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.View.layout(View.java:7175)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.View.layout(View.java:7175)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.View.layout(View.java:7175)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.View.layout(View.java:7175)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.os.Looper.loop(Looper.java:123)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at java.lang.reflect.Method.invoke(Method.java:507)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-08 23:55:25.361: E/AndroidRuntime(23627):    at dalvik.system.NativeStart.main(Native Method)
01-09 00:00:25.431: I/Process(23627): Sending signal. PID: 23627 SIG: 9
4

1 回答 1

0

您不填充整个executions数组:

executions = new String[allExecutionsFromDatabase.size()];

当你迭代直到

for (int i = 0; i < allExecutionsFromDatabase.size() - 1; i++) {

中的最后一个索引条目executions仍然为空。

这是第一次看到 - 不确定这是否是空指针 exc 的原因,也是唯一的原因:)。

于 2013-01-09T00:23:05.633 回答