我按下第一项

时间:2018-03-07 20:36:54

标签: android android-intent android-actionbar

我写了一个动作栏。这太简单了。当我点击某人时,他们会进行相同的活动。意图消息之间只有不同之处。我一如既往地做了。我的问题是,当我点击第一个项目给我一个例外,但第二个是纯粹的工作。我做错了什么?我该如何解决?

我的目的是:我想在我的行动栏的项目中发送此活动的消息.İf消息123我将做任何事情,但如果其他项目已点击我做了不同的事情。我只是想找到哪个项目被用户点击。

Atolye_action_bar.class:

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.muddzdev.styleabletoastlibrary.StyleableToast;

public class Atolyem_action_bar extends AppCompatActivity {

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





        Bundle exras = getIntent().getExtras();
        String value = exras.getString("gzsz");
        String value_anlamsız_ıntent_message = exras.getString("anlamsız");


        if (value.equals("123")){
            new StyleableToast
                    .Builder(getApplicationContext())
                    .text("Doğru")
                    .textColor(Color.BLACK)
                    .backgroundColor(Color.RED)
                    .show();


        }




    }
}
消息的形状:

Bundle yeni = getIntent().getExtras();
        String value_anlamsız_ıntent_message = yeni.getString("anlamsız");


        Bundle exras = getIntent().getExtras();
        String value = exras.getString("gzsz");


// İt is true.
        if (value != null && value.equals("123")){
            new StyleableToast
                    .Builder(getApplicationContext())
                    .text("Doğru")
                    .textColor(Color.BLACK)
                    .backgroundColor(Color.RED)
                    .show();


        }
// it is not working.
        if (value_anlamsız_ıntent_message != null && value_anlamsız_ıntent_message.equals("selam")){

            new StyleableToast
                    .Builder(getApplicationContext())
                    .text("yalan")
                    .textColor(Color.BLACK)
                    .backgroundColor(Color.RED)
                    .show();






        }

这是我的项目:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="Sadace Anlamsız kelimeler"
        android:id="@+id/anlamsız_atolyem"
        app:showAsAction="never"
        android:orderInCategory="100"
        />
    <item android:title="Sadace Gözsüz kelimeler"
        android:id="@+id/gözsüz_atolyem"
        app:showAsAction="never"
        android:orderInCategory="100"/>
</menu>

我的代码:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.atolyem_for,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id ==R.id.anlamsız_atolyem) {   
            anlamsız();
            return true;
        }

        if (id ==R.id.gözsüz_atolyem  ){
            gzsz(); return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void gzsz() {
        String gzsz = "123";

        Intent gzsz_intent = new Intent(Atolyem.this, Atolyem_action_bar.class);
        gzsz_intent.putExtra( "gzsz",gzsz );
        startActivity(gzsz_intent);
    }

    private void anlamsız() {
        String anlamsiz = "selam";

        Intent anlamsız_intent = new Intent(Atolyem.this, Atolyem_action_bar.class);
         anlamsız_intent.putExtra( "anlamsiz",anlamsiz);
         startActivity(anlamsız_intent);
    }

这是我的例外:

03-07 23:44:24.717 3654-3654/something.about.berkay.kkk E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: something.about.berkay.kkk, PID: 3654
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{something.about.berkay.kkk/something.about.berkay.kkk.Atolyem_action_bar}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                              at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                              at something.about.berkay.kkk.Atolyem_action_bar.onCreate(Atolyem_action_bar.java:25)
                                                                              at android.app.Activity.performCreate(Activity.java:6955)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                              at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

1 个答案:

答案 0 :(得分:1)

如果没有设置关键字“gzsz”,则

value为空。在调用equals之前执行null检查:

if (value != null && value.equals("123")){
...