Android意图错误null对象引用

时间:2016-12-06 16:13:10

标签: java android nullpointerexception

你好我在stackover流程中的新功能我想尝试这些网站并在我的android中请求某人的帮助当我更改意图时我的android工作室中的android监视器中出现错误就在这里。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.kim.checkerv3, PID: 2970
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kim.checkerv3/com.example.kim.checkerv3.BudgetOutput}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
                  at com.example.kim.checkerv3.BudgetOutput.onCreate(BudgetOutput.java:22)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我不明白这个我试过研究解决方案而我仍然坚持它。

这里的意图将传递给其他java类。

package com.example.kim.checkerv3;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by KIM-PC on 12/5/2016.
 */

public class BudgetFragment extends android.support.v4.app.Fragment {
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_budget, container, false);

        final EditText etCash = (EditText) v.findViewById(R.id.etCash);
        final Button bBudget = (Button) v.findViewById(R.id.bBudget);

        bBudget.setOnClickListener(new View.OnClickListener(){


            public void onClick(View v) {
                SharedPreferences sharedpref = getContext().getSharedPreferences("user_id", MODE_PRIVATE);
                String id = sharedpref.getString("user_id","");

                final int budgeted = Integer.parseInt(etCash.getText().toString());

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {

                            JSONObject jsonResponse = new JSONObject(response);
                            JSONArray arr = jsonResponse.getJSONArray("message");
                            boolean success = jsonResponse.getBoolean("success");

                            System.out.println(budgeted/arr.length());
                            int total = budgeted/arr.length();

                            if (success) {

                                Intent intent = new Intent(getContext(), BudgetOutput.class);
                                Bundle b = new Bundle();
                                b.putString("total", String.valueOf(total));
                                getContext().startActivity(intent);

                                if (arr != null) {
                                    int sum = 0;
                                    for(int i = 0;i<arr.length();i++){
                                        //listdata.add(arr.get(i).toString());
                                        String money = arr.getJSONObject(i).getString("amount");
                                        sum += Integer.parseInt(money);



                                    }
                                    SharedPreferences sharedPref1 = getActivity().getSharedPreferences("mypref", Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sharedPref1.edit();
                                    editor.putInt("TotalSum", sum);
                                    editor.commit();
                                    System.out.println(sum);








                                    //arrayAdapter.notifyDataSetChanged();
                                }



                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                                builder.setMessage("Failed to fetch. Try again.")
                                        .setNegativeButton("OK", null)
                                        .create()
                                        .show();
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                };
                RecordRequest recordRequest = new RecordRequest(id, responseListener);
                RequestQueue queue = Volley.newRequestQueue(getContext());
                queue.add(recordRequest);




            }

        });



        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        SharedPreferences sharedpref1 = getContext().getSharedPreferences("mypref", MODE_PRIVATE);
        int budgeted = sharedpref1.getInt("TotalSum", 0);





        System.out.println(budgeted/2);

    }
}

以及意图获得的类。

package com.example.kim.checkerv3;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by KIM-PC on 12/6/2016.
 */
public class BudgetOutput extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_output);

        final TextView tvOutput = (TextView) findViewById(R.id.tvoutput);

        Bundle b = getIntent().getExtras();
        String output=b.getString("total");

        if(b !=null)
            output = b.getString("total");

        tvOutput.setText(output+" Per cheque record.");

    }
}

如果有人可以解决我欣赏并感谢你。

1 个答案:

答案 0 :(得分:1)

好吧,您忘了将Bundle添加到Intent

Intent intent = new Intent(getContext(), BudgetOutput.class);
Bundle b = new Bundle();
b.putString("total", String.valueOf(total));
intent.putExtras(b);
getContext().startActivity(intent);