2015-06-20 142 views
-2

我試了好幾個小時纔得到這個工作。我試圖將電池信息從活動傳遞到片段。我不斷得到一個NullPointerException。Android - 將數據從活動傳遞到片段

java.lang.NullPointerException: Attempt to invoke virtual method 'int 
android.os.Bundle.getInt(java.lang.String)' on a null object reference 

我看了很多在互聯網上的其他用戶問題,我還沒有找到解決方案。 我的代碼片段:

package natanrosenfeld.systeminfo; 

import android.annotation.TargetApi; 
import android.os.BatteryManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

/** 
* Created by natanrosenfeld on 6/18/15. 
*/ 
@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public class BatteryInfoFragment extends Fragment { 

/*private String [] batteryProperties = {"Battery Plugged AC: " + 
BatteryManager.BATTERY_PLUGGED_AC, 
     "Battery Plugged USB: " + BatteryManager.BATTERY_PLUGGED_USB, 
"Battery Plugged Wireless: " + BatteryManager.BATTERY_PLUGGED_WIRELESS, 
"Battery Capacity: " + BatteryManager.BATTERY_PROPERTY_CAPACITY, "Battery 
Charge Counter: " + BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 
"Battery Current Average: " + 
BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, "Battery Current Now: " + 
BatteryManager.BATTERY_PROPERTY_CURRENT_NOW, 
"Battery Energy Counter: " + BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER, 
"Battery Status Charging: " + BatteryManager.BATTERY_STATUS_CHARGING, 
"Battery Status Discharging: " + BatteryManager.BATTERY_STATUS_DISCHARGING, 
"Battery Status Full: " + BatteryManager.BATTERY_STATUS_FULL, 
     "Battery Status Unknown: " + BatteryManager.BATTERY_STATUS_UNKNOWN, 
"Battery Health: " + "", 
"Battery Icon Small: " + BatteryManager.EXTRA_ICON_SMALL, "Battery Level: " 
+ BatteryManager.EXTRA_LEVEL, "Is Battery Plugged: " + 
BatteryManager.EXTRA_PLUGGED, 
"Is Battery Present: " + BatteryManager.EXTRA_PRESENT, "Battery Scale: " + 
BatteryManager.EXTRA_SCALE, "Battery Status: " + 
BatteryManager.EXTRA_STATUS, 
"Battery Technology: " + BatteryManager.EXTRA_TECHNOLOGY, "Battery 
Temperature: " + BatteryManager.EXTRA_TEMPERATURE, "Battery Voltage: " + 
BatteryManager.EXTRA_VOLTAGE}; 
*/ 
private ListView batteryPropsView; 
private ArrayAdapter arrayAdapter; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    //Bundle bundle = this.getArguments(); 
    int myValue = getArguments().getInt("message"); 


    String [] batteryProperties = {"Battery Plugged AC: " + 
BatteryManager.BATTERY_PLUGGED_AC, 
      "Battery Plugged USB: " + BatteryManager.BATTERY_PLUGGED_USB, 
"Battery Plugged Wireless: " + BatteryManager.BATTERY_PLUGGED_WIRELESS, 
      "Battery Capacity: " + BatteryManager.BATTERY_PROPERTY_CAPACITY, 
"Battery Charge Counter: " + BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 
      "Battery Current Average: " + 
BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, "Battery Current Now: " + 
BatteryManager.BATTERY_PROPERTY_CURRENT_NOW, 
      "Battery Energy Counter: " + 
BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER, "Battery Status Charging: " 
+ BatteryManager.BATTERY_STATUS_CHARGING, 
      "Battery Status Discharging: " + 
BatteryManager.BATTERY_STATUS_DISCHARGING, "Battery Status Full: " + 
BatteryManager.BATTERY_STATUS_FULL, 
      "Battery Status Unknown: " + 
BatteryManager.BATTERY_STATUS_UNKNOWN, "Battery Health: " + myValue, 
      "Battery Icon Small: " + BatteryManager.EXTRA_ICON_SMALL, 
"Battery Level: " + BatteryManager.EXTRA_LEVEL, "Is Battery Plugged: " + 
BatteryManager.EXTRA_PLUGGED, 
      "Is Battery Present: " + BatteryManager.EXTRA_PRESENT, "Battery 
Scale: " + BatteryManager.EXTRA_SCALE, "Battery Status: " + 
BatteryManager.EXTRA_STATUS, 
      "Battery Technology: " + BatteryManager.EXTRA_TECHNOLOGY, 
"Battery Temperature: " + BatteryManager.EXTRA_TEMPERATURE, "Battery 
Voltage: " + BatteryManager.EXTRA_VOLTAGE}; 
    View rootView = inflater.inflate(R.layout.fragment_batteryinfo, 
container, false); 
    batteryPropsView = (ListView) rootView.findViewById(R.id.batteryInfo); 

    arrayAdapter = new ArrayAdapter(getActivity(), 
android.R.layout.simple_list_item_1, batteryProperties); 
    batteryPropsView.setAdapter(arrayAdapter); 



    batteryPropsView.setOnItemLongClickListener(new 
AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int 
position, 
             long id) { 
      Toast.makeText(getActivity(), "You clicked on an item.", 
Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
    }); 
    return rootView; 
} 


} 

我的活動代碼:

package natanrosenfeld.systeminfo; 

/** 
* Created by natanrosenfeld on 6/19/15. 
*/ 
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.BatteryManager; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.TextView; 



public class BatteryStatsActivity extends Activity { 
private TextView batteryInfo; 
private ImageView imageBatteryState; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.registerReceiver(this.batteryInfoReceiver, new 
IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 


} 


private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     int health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); 
     int icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0); 
     int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); 
     int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); 
     boolean present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT); 
     int scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0); 
     int status= intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0); 
     String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY); 
     int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0); 
     int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); 


     Bundle bundle = new Bundle(); 
     bundle.putInt("message", health); 
     // set Fragmentclass Arguments 
     BatteryInfoFragment fragobj = new BatteryInfoFragment(); 
     fragobj.setArguments(bundle); 
     getFragmentManager().beginTransaction() 
     .addToBackStack(null) 
     .commit(); 


     batteryInfo.setText(
       "Health: "+health+"\n"+ 
         "Icon Small:"+icon_small+"\n"+ 
         "Level: "+level+"\n"+ 
         "Plugged: "+plugged+"\n"+ 
         "Present: "+present+"\n"+ 
         "Scale: "+scale+"\n"+ 
         "Status: "+status+"\n"+ 
         "Technology: "+technology+"\n"+ 
         "Temperature: "+temperature+"\n"+ 
         "Voltage: "+voltage+"\n"); 
     imageBatteryState.setImageResource(icon_small); 
    } 
}; 


} 

任何幫助將不勝感激!我不想花我的應用程序只有這一部分的一天!謝謝!

+0

但是,在你加入'getFragmentManager()調用BeginTransaction()''fragment'? –

+0

@MD,我將它添加到Activity類中:getFragmentManager()。beginTransaction() .addToBackStack(null) .commit(); –

+0

試試這個'getFragmentManager()。beginTransaction(android.R.id.container).add(fragobj);' –

回答

0

從你的活動意圖爲發送數據:

Bundle bundle = new Bundle(); 
bundle.putString("edttext", "From Activity"); 

// set Fragmentclass Arguments 

Fragmentclass fragobj = new Fragmentclass(); 

fragobj.setArguments(bundle); 

和片段onCreateView方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
String strtext = getArguments().getString("edttext");  
return inflater.inflate(R.layout.fragment, container, false); 
} 
+0

我試過這個但它不起作用。 –

+0

我可以用獨立的代碼顯示你的嘗試嗎? –

+0

是的,如果你想 –

相關問題