无法使“保存”按钮起作用

时间:2014-06-21 21:00:59

标签: java android

我正在尝试使用java在Android开发上使用PluralSight提供的课程,在课程中你正在创建一个类似app的记事本,我试图让保存按钮工作,我正在使用他们在教程中使用的代码相同,但由于某种原因它不适合我,我得到一个空指针异常,谢谢!

以下是代码:

package com.example.notetaker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        final Button saveButton = (Button)findViewById(R.id.saveButton);

        saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                saveButton.setText("clicked!");

            }
        });
    }


    @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);
    }

    /**
     * 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:

06-21 17:56:26.713:D / AndroidRuntime(1502):关闭VM 06-21 17:56:26.713:W / dalvikvm(1502):threadid = 1:线程退出未捕获异常(group = 0xb3b0dba8) 06-21 17:56:26.743:E / AndroidRuntime(1502):致命异常:主要 06-21 17:56:26.743:E / AndroidRuntime(1502):进程:com.example.notetaker,PID:1502 06-21 17:56:26.743:E / AndroidRuntime(1502):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.notetaker / com.example.notetaker.MainActivity}:java.lang.NullPointerException 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.os.Handler.dispatchMessage(Handler.java:102) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.os.Looper.loop(Looper.java:136) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread.main(ActivityThread.java:5017) 06-21 17:56:26.743:E / AndroidRuntime(1502):at java.lang.reflect.Method.invokeNative(Native Method) 06-21 17:56:26.743:E / AndroidRuntime(1502):at java.lang.reflect.Method.invoke(Method.java:515) 06-21 17:56:26.743:E / AndroidRuntime(1502):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 06-21 17:56:26.743:E / AndroidRuntime(1502):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-21 17:56:26.743:E / AndroidRuntime(1502):at dalvik.system.NativeStart.main(Native Method) 06-21 17:56:26.743:E / AndroidRuntime(1502):引起:java.lang.NullPointerException 06-21 17:56:26.743:E / AndroidRuntime(1502):at com.example.notetaker.MainActivity.onCreate(MainActivity.java:29) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.Activity.performCreate(Activity.java:5231) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-21 17:56:26.743:E / AndroidRuntime(1502):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-21 17:56:26.743:E / AndroidRuntime(1502):... 11更多

1 个答案:

答案 0 :(得分:0)

看看你的NullPointerException,它应该告诉你哪一行你有空指针。没有这些信息,很难帮助你。此代码中有一些地方可能会发生NullPointer,例如,如果找不到View。