我已经尝试了几个小时才能让它发挥作用。我试图将电池信息从活动传递到片段。我一直收到NullPointerException。
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 :(得分:0)
从活动中您按意图发送数据:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
并在Fragment onCreateView方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}