2016-12-06 48 views
-1

你好我新在stackover流我想尝试这些网站,并要求在我的机器人someones帮助,当我改变意图有一个错误在android工作室显示器在android工作室这里是它。Android意图错误null对象引用

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

好了,你忘了添加BundleIntent

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