如何解决从菜单启动Activity时的崩溃问题

时间:2015-10-12 12:00:22

标签: android android-intent android-activity nullpointerexception forceclose

我添加了菜单,在项目菜单中我已将Intent添加到某个Activity中,例如SettingActivity,但是我的应用程序崩溃但是当其他活动启动时,它并没有崩溃。

我已经使用SharedPreferences

在I类中添加了Setting类
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_preferences:
        Intent intent0 = new Intent(this, Setting.class);
        this.startActivity(intent0);
        break;
    default:
        return super.onOptionsItemSelected(item);
    }
    return true;
}

public class Setting extends Activity {
    int back;
    private String mood;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_page);
        final TextView sampletxt = (TextView) findViewById(R.id.textView4);
        SharedPreferences of = getApplicationContext().getSharedPreferences("offon", 0);
        final SharedPreferences.Editor on = of.edit();
        int onf = of.getInt("ofon", 0);
        final Button bs = (Button) findViewById(R.id.bon);

        SharedPreferences size = getApplicationContext().getSharedPreferences("size1", 0);
        final SharedPreferences.Editor editor1 = size.edit();
        final int ssize = size.getInt("size2", 0);

        SharedPreferences color = getApplicationContext().getSharedPreferences("color1", 0);
        final SharedPreferences.Editor editor2 = color.edit();
        final int scolor = color.getInt("color2", 0);

        SharedPreferences fontq = getApplicationContext().getSharedPreferences("font1a", 0);
        final SharedPreferences.Editor editor3 = fontq.edit();
        final int sfont = fontq.getInt("font2a", 0);

        SeekBar sf = (SeekBar) findViewById(R.id.seekBar1);

        sf.setMax(4);
        sf.setProgress(sfont);
        if (sfont == 0 || sfont == 1) {
            sf.setProgress(1);
            sampletxt.setTypeface(type1);
        } else if (sfont == 2) {
            sf.setProgress(2);
            sampletxt.setTypeface(type2);
        } else if (sfont == 3) {
            sf.setProgress(3);
            sampletxt.setTypeface(type3);
        } else if (sfont == 4) {
            sf.setProgress(4);
            sampletxt.setTypeface(type4);
        } else if (sfont == 5) {
            sf.setProgress(5);
            sampletxt.setTypeface(type5);
        }

        red.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                sampletxt.setTextColor(Color.RED);
                editor2.putInt("color2", 1);
                editor2.commit();
            }
        });

        s_size.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {

                sampletxt.setTextSize(arg1);
                editor1.putInt("size2", arg1);
                editor1.commit();

            }
        });
        sf.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
            }

        });
    }
}

这是坠机事件。

10-12 16:22:09.663: ERROR/AndroidRuntime(740): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Zaban/com.example.Zaban.Setting}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
        at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.example.Zaban.Setting.onCreate(Setting.java:89)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
        ... 11 more
10-12 16:22:11.772: ERROR/InputDispatcher(96): channel '4063be30 com.example.Zaban/com.example.Zaban.MyActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
10-12 16:22:11.772: ERROR/InputDispatcher(96): channel '4063be30 com.example.Zaban/com.example.Zaban.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-12 16:22:12.542: ERROR/InputDispatcher(96): Received spurious receive callback for unknown input channel.  fd=149, events=0x8
10-12 16:22:12.552: ERROR/InputDispatcher(96): Received spurious receive callback for unknown input channel.  fd=155, events=0x8

1 个答案:

答案 0 :(得分:0)

您可以为startnewactivity创建一个函数,并在菜单中调用该函数。

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.home:
            openNewIntent(YourClass.class);
            return true;
}}


private void openNewIntent(Class a)
{
    Intent i = new Intent(this, a);
    startActivity(i);
}