在新 activity 错误 null 异常中打开意图时应用程序崩溃?

app crashes when opening intent in new activity error nullexception?

我对这一切都很陌生,但我对一些我无法弄清楚的事情感到很困惑。我查看了许多以前提出的问题,但无济于事。我正在尝试将一个值从列表视图传递到新的 activity。我正在使用意图,选定的值确实得到了很好的注册(在我的 mainactivity 和日志中用一个简单的文本视图检查了它。但是当我想在我的新 activity 中检索它们时应用程序崩溃。所以我想问题出在我的第二个 activity 中的某个地方。非常感谢帮助。(ps。我的代码可能需要清理,但稍后我会解决这个问题。) 我的 main_activity:

   public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    EditText logedit;
    EditText logexistedit;
    Button logbutton;
    Button logexistbutton;
    ListView lognaamlist;
    ArrayAdapter userArrayAdapter;
    ArrayList userList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logedit=(EditText)findViewById(R.id.logedit);
        logbutton = (Button) findViewById(R.id.logbutton);
        logbutton.setOnClickListener(this);
        lognaamlist=(ListView)findViewById(R.id.listView);
        logexistbutton = (Button)findViewById(R.id.existlogbutton);
        logexistbutton.setOnClickListener(this);
        lognaamlist = (ListView) findViewById(R.id.listView);
        Firebase.setAndroidContext(this);


       userArrayAdapter = new ArrayAdapter<String>(this,
                R.layout.activity_loginrow, R.id.loginnaam,
                userList);
        LoadPreferences();

        lognaamlist.setAdapter(userArrayAdapter);
        lognaamlist.setOnItemClickListener(this);
       }

    protected void SavePreferences(String key, String value) {
        // TODO Auto-generated method stub
        SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = data.edit();
        editor.putString(key, value);
        editor.commit();
    }
    protected void LoadPreferences(){
        SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
        String dataSet = data.getString("LISTS", "None Available");
        userArrayAdapter.add(dataSet);
        userArrayAdapter.notifyDataSetChanged();
    }

        private void actienaam(String String) {
        Firebase ref = new Firebase("https://packagename.firebaseio.com/lijstapp");


       String savename = (((logedit.getText().toString())));
       Firebase userRef = ref.child("users");
        Map<String, Object> rec1 = new HashMap<String, Object>();
        rec1.put(savename, savename);
        userRef.updateChildren(rec1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.logbutton:
                actienaam(((logedit.getText().toString())));
                // Also add that value to the list shown in the ListView
                userList.add(logedit.getText().toString());
                userArrayAdapter.notifyDataSetChanged();
                SavePreferences("LISTS", (logedit.getText().toString()));
                break;

            case R.id.existlogbutton:

                userList.add(logedit.getText().toString());
                userArrayAdapter.notifyDataSetChanged();
                SavePreferences("LISTS", (logedit.getText().toString()));
                break;
            default:
                break;
        }


    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

     String userdata = (String) userList.get(position);

        Intent i = new Intent(MainActivity.this, Keuzelijstactivity.class);

        i.putExtra("userdata", userdata);
        startActivity(i);

        // Log the item's position and contents
        // to the console in Debug
        //later veranderen in volgende stap
        Log.d("omg android", position + ": " + userList.get(position));
    }}

我的第二个activity:

  public class Keuzelijstactivity extends Activity {
    TextView boodschaptext;
    TextView takentext;
    TextView test;
    String userdata;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boodschaptext = (TextView) findViewById(R.id.boodschappenlijst);
        takentext = (TextView) findViewById(R.id.takenlijst);
        test = (TextView)findViewById(R.id.test);
        setContentView(R.layout.lijsten_layout);
        Firebase.setAndroidContext(this);

       Intent i = getIntent();
      userdata= i.getStringExtra("userdata");
      test.setText(userdata);
    }

最后我的 logcat 崩溃时:

01-26 22:21:13.205      834-834/prolijst.rob.com.prolijst E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{prolijst.rob.com.prolijst/prolijst.rob.com.prolijst.Keuzelijstactivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access0(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            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:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at prolijst.rob.com.prolijst.Keuzelijstactivity.onCreate(Keuzelijstactivity.java:30)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access0(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            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:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

问题就在这里

boodschaptext = (TextView) findViewById(R.id.boodschappenlijst);
takentext = (TextView) findViewById(R.id.takenlijst);
test = (TextView)findViewById(R.id.test);
setContentView(R.layout.lijsten_layout);

您在调用 setContentView(R.layout.lijsten_layout); 之前已经初始化了您的视图,这就是您收到 NPE 异常的原因。所以只需将其更改为

 setContentView(R.layout.lijsten_layout);
 boodschaptext = (TextView) findViewById(R.id.boodschappenlijst);
 takentext = (TextView) findViewById(R.id.takenlijst);
 test = (TextView)findViewById(R.id.test);