Android无法更改视图

时间:2013-07-11 01:27:36

标签: android android-intent

所以我对android很新,我在改变视图时遇到了问题。我有一个简单的任务列表应用程序,我希望能够在单击时打开“详细信息”视图。这就是我所拥有的:

public class ViewTask extends Activity {
    protected TaskerDbHelper db;
    private List<Task> list;
    private MyAdapter adapt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_task);
        //db is a variable of type TaskerDbHelper
        db = new TaskerDbHelper(this);
        list = db.getAllTasks();
        adapt = new MyAdapter(this, R.layout.list_inner_view , list);
        ListView listTask = (ListView)findViewById(R.id.listView1);

        listTask.setAdapter(adapt);
        listTask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String taskName = (String)((ViewHolder)view.getTag()).taskname.getText();
                Toast.makeText(getApplicationContext(), "Clicked: " + taskName, Toast.LENGTH_LONG).show();
                Intent myIntent = new Intent(getApplicationContext(), Details.class);
                myIntent.putExtra("TASK_NAME", taskName);
                startActivity(myIntent);
            }
        });
    }
....
}

当我实际点击任务时,我收到此错误:

07-11 00:54:58.671    1918-1918/com.example.tasker             E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tasker/com.example.tasker.Details}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.example.tasker.Details.onCreate(Details.java:17)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试使用String taskName = (String) adapter.getItem(i)代替view.getTag行。这就是你检索行内容的方法。