在为listview制作上下文菜单时,应用程序停止工作

时间:2014-05-23 20:05:40

标签: android listview

正如标题所示,在我为listview创建了一个上下文菜单后,我的app停止了工作。对于Eclipse来说,我是一个新手,但我根本无法理解为什么它会阻止工作。 这是我的代码:

package dk.miracon.bodybuilderlog;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

    Button listView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.app_name);
        setContentView(R.layout.activity_main);
        listView1 = (Button)findViewById(R.id.listView1);

        registerForContextMenu(listView1);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case R.id.item1:
            listView1.setText("Rediger");
            break;
        case R.id.item2:
            listView1.setText("Slet");
            break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflate = getMenuInflater();
        inflate.inflate(R.menu.context, menu);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void buttonClickOpretWorkout(View v)
    {
        Intent i = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(i);
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

这是logcat

     05-23 21:47:10.851: E/Trace(29873): error opening trace file: No such file or directory (2)
     05-23 21:47:10.911: W/dalvikvm(29873): threadid=1: thread exiting with uncaught exception (group=0x41e3c390)
     05-23 21:47:10.911: E/AndroidRuntime(29873): FATAL EXCEPTION: main
     05-23 21:47:10.911: E/AndroidRuntime(29873): java.lang.RuntimeException: Unable to start activity
 ComponentInfo{dk.miracon.bodybuilderlog/dk.miracon.bodybuilderlog.MainActivity}:
 java.lang.NullPointerException
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread.access$600(ActivityThread.java:151)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.os.Handler.dispatchMessage(Handler.java:99)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.os.Looper.loop(Looper.java:155)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread.main(ActivityThread.java:5511)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at java.lang.reflect.Method.invokeNative(Native Method)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at java.lang.reflect.Method.invoke(Method.java:511)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at dalvik.system.NativeStart.main(Native Method)
     05-23 21:47:10.911: E/AndroidRuntime(29873): Caused by: java.lang.NullPointerException
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.Activity.registerForContextMenu(Activity.java:2896)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at dk.miracon.bodybuilderlog.MainActivity.onCreate(MainActivity.java:29)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.Activity.performCreate(Activity.java:5066)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
     05-23 21:47:10.911: E/AndroidRuntime(29873):   ... 11 more
     05-23 21:56:43.960: E/Trace(30022): error opening trace file: No such file or directory (2)
     05-23 21:56:44.030: W/dalvikvm(30022): threadid=1: thread exiting with uncaught exception (group=0x41e3c390)
     05-23 21:56:44.030: E/AndroidRuntime(30022): FATAL EXCEPTION: main
     05-23 21:56:44.030: E/AndroidRuntime(30022): java.lang.RuntimeException: Unable to start activity
 ComponentInfo{dk.miracon.bodybuilderlog/dk.miracon.bodybuilderlog.MainActivity}:
 java.lang.NullPointerException
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread.access$600(ActivityThread.java:151)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.os.Handler.dispatchMessage(Handler.java:99)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.os.Looper.loop(Looper.java:155)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread.main(ActivityThread.java:5511)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at java.lang.reflect.Method.invokeNative(Native Method)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at java.lang.reflect.Method.invoke(Method.java:511)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at dalvik.system.NativeStart.main(Native Method)
     05-23 21:56:44.030: E/AndroidRuntime(30022): Caused by: java.lang.NullPointerException
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.Activity.registerForContextMenu(Activity.java:2896)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at dk.miracon.bodybuilderlog.MainActivity.onCreate(MainActivity.java:29)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.Activity.performCreate(Activity.java:5066)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
     05-23 21:56:44.030: E/AndroidRuntime(30022):   ... 11 more
     05-23 21:57:11.467: E/Trace(30108): error opening trace file: No such file or directory (2)
     05-23 21:57:11.537: W/dalvikvm(30108): threadid=1: thread exiting with uncaught exception (group=0x41e3c390)
     05-23 21:57:11.537: E/AndroidRuntime(30108): FATAL EXCEPTION: main
     05-23 21:57:11.537: E/AndroidRuntime(30108): java.lang.RuntimeException: Unable to start activity
 ComponentInfo{dk.miracon.bodybuilderlog/dk.miracon.bodybuilderlog.MainActivity}:
 java.lang.NullPointerException
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread.access$600(ActivityThread.java:151)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.os.Handler.dispatchMessage(Handler.java:99)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.os.Looper.loop(Looper.java:155)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread.main(ActivityThread.java:5511)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at java.lang.reflect.Method.invokeNative(Native Method)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at java.lang.reflect.Method.invoke(Method.java:511)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at dalvik.system.NativeStart.main(Native Method)
     05-23 21:57:11.537: E/AndroidRuntime(30108): Caused by: java.lang.NullPointerException
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.Activity.registerForContextMenu(Activity.java:2896)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at dk.miracon.bodybuilderlog.MainActivity.onCreate(MainActivity.java:29)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.Activity.performCreate(Activity.java:5066)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
     05-23 21:57:11.537: E/AndroidRuntime(30108):   ... 11 more

我希望你们能帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我猜是因为你没有以正确的方式获得列表:

listView1 = (Button)findViewById(R.id.listView1);

您正在将列表视图转换为Button,因此它为null并且您尝试为空对象注册上下文菜单。

Button更改为ListView(演员和变量声明)。

另外请确保您使用的ID正确无误。