此活动正常运作。但我需要将Activity作为片段。我试图将活动改为碎片,但它没有工作......所以有人可以帮助我改变吗?
Animals
我尝试将代码自行转换为片段,代码如下: 转换后得到的结果:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.mobilemerit.batterychecker.App;
import com.mobilemerit.java.GetBatteryStats;
public class BatteryCheck extends Activity {
private TextView bLevel, header, health, voltage, tech, temp,
heath_tag,temp_tag,tech_tag,voltage_tag;
private ProgressBar bProgress;
private ImageButton cheakUsage;
private int level;
private Intent usageIntent;
private ResolveInfo resolveInfo;
private Typeface myTypeface,secondrayTypeFace;
private Tracker tracker;
private GoogleAnalytics gTracker;
private ImageButton batteryTester;
public static final String LEVEL="level";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery_check_new);
/**
* feeding the context of thw application
* So as to use this at various places
*
* */
new App(getApplicationContext());
// Attaching the Font type with the Text Views
myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Franklin Gothic.otf");
secondrayTypeFace=Typeface.createFromAsset(getAssets(),"fonts/GOTHIC.TTF" );
/**
* Google Analytic code
*/
gTracker = GoogleAnalytics.getInstance(getApplicationContext());
tracker = gTracker.getTracker("UA-41735784-1");
// Finding the XML Views
bLevel = (TextView) findViewById(R.id.blevel1);
bProgress = (ProgressBar) findViewById(R.id.progressBar);
cheakUsage = (ImageButton) findViewById(R.id.usage);
header = (TextView) findViewById(R.id.header);
tech = (TextView) findViewById(R.id.technology);
temp = (TextView) findViewById(R.id.tempreture);
voltage = (TextView) findViewById(R.id.voltage);
health = (TextView) findViewById(R.id.health);
/**find up the tags view(of stats textviews) as well to set up the typeface */
heath_tag=(TextView)this.findViewById(R.id.health_tag);
tech_tag=(TextView)this.findViewById(R.id.technology_tag);
temp_tag=(TextView)this.findViewById(R.id.temp_tag);
voltage_tag=(TextView)this.findViewById(R.id.voltage_tag);
heath_tag.setTypeface(secondrayTypeFace);
tech_tag.setTypeface(secondrayTypeFace);
temp_tag.setTypeface(secondrayTypeFace);
voltage_tag.setTypeface(secondrayTypeFace);
/** Code for Google Admob */
bLevel.setTypeface(myTypeface);
header.setTypeface(myTypeface);
// register the Broadcast Receiver
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
// Setting up the Intent for the Power Usage details
usageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
// Check it for presence
resolveInfo = getPackageManager().resolveActivity(usageIntent, 0);
if (resolveInfo == null) {
Toast.makeText(this, "Not Support!", Toast.LENGTH_LONG).show();
cheakUsage.setEnabled(false);
} else {
cheakUsage.setEnabled(true);
}
// Setting up the functionality of the Button Clicks
cheakUsage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(usageIntent);
}
});
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
setUpStats(intent);
// Toast.makeText(getBaseContext(),
// ""+intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,-1),Toast.LENGTH_SHORT).show();
new ShowProgressClass().execute();
}
};
public void setUpStats(Intent intent) {
GetBatteryStats stats = new GetBatteryStats(intent);
health.setText("" + stats.getBatteryHealth());
tech.setText("" + stats.getBatteryTechnology());
temp.setText("" + stats.getBatteryTempreture());
voltage.setText("" + stats.getBatteryVoltage());
//Setting up typefaces for the stats
health.setTypeface(secondrayTypeFace);
tech.setTypeface(secondrayTypeFace);
temp.setTypeface(secondrayTypeFace);
voltage.setTypeface(secondrayTypeFace);
/**Save current voltage and level to calculate the Remaining time on Battery*/
SharedPreferences prefs=this.getSharedPreferences(LEVEL, Context.MODE_PRIVATE);
prefs.edit().putString("level",""+level);
prefs.edit().putString("voltage",stats.getBatteryVoltage());
}
// For Upadating progress bar After loading
class ShowProgressClass extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < level; i++) {
publishProgress(1);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPreExecute() {
bProgress.setMax(100);
bProgress.setProgress(0);
// TODO Auto-generated method stub
}
@Override
protected void onProgressUpdate(Integer... values) {
bProgress.incrementProgressBy(values[0]);
bLevel.setText(" " + bProgress.getProgress() + "%");
// TODO Auto-generated method stub
}
}
@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Alert");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to quit?");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onDestroy();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
return super.onKeyDown(keycode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
但是当我尝试运行时,我会遇到以下错误:
package com.raihanbd.easyrambooster;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.mobilemerit.batterychecker.App;
import com.mobilemerit.java.GetBatteryStats;
public class BatteryCheck extends Fragment implements OnClickListener {
private TextView bLevel, header, health, voltage, tech, temp,
heath_tag,temp_tag,tech_tag,voltage_tag;
private ProgressBar bProgress;
private ImageButton cheakUsage;
private int level;
private Intent usageIntent;
private ResolveInfo resolveInfo;
private Typeface myTypeface,secondrayTypeFace;
private Tracker tracker;
private GoogleAnalytics gTracker;
private ImageButton batteryTester;
public static final String LEVEL="level";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.battery_check_new, container, false);
/**
* feeding the context of thw application
* So as to use this at various places
*
* */
new App(getActivity().getApplicationContext());
// Attaching the Font type with the Text Views
// myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Franklin Gothic.otf");
// secondrayTypeFace=Typeface.createFromAsset(getAssets(),"fonts/GOTHIC.TTF" );
/**
* Google Analytic code
*/
gTracker = GoogleAnalytics.getInstance(getActivity().getApplicationContext());
tracker = gTracker.getTracker("UA-41735784-1");
// Finding the XML Views
bLevel = (TextView) getView().findViewById(R.id.blevel1);
bProgress = (ProgressBar) getView().findViewById(R.id.progressBar);
cheakUsage = (ImageButton) getView().findViewById(R.id.usage);
header = (TextView) getView().findViewById(R.id.header);
tech = (TextView) getView().findViewById(R.id.technology);
temp = (TextView) getView().findViewById(R.id.tempreture);
voltage = (TextView) getView().findViewById(R.id.voltage);
health = (TextView) getView().findViewById(R.id.health);
/**find up the tags view(of stats textviews) as well to set up the typeface */
heath_tag=(TextView)this.getView().findViewById(R.id.health_tag);
tech_tag=(TextView)this.getView().findViewById(R.id.technology_tag);
temp_tag=(TextView)this.getView().findViewById(R.id.temp_tag);
voltage_tag=(TextView)this.getView().findViewById(R.id.voltage_tag);
heath_tag.setTypeface(secondrayTypeFace);
tech_tag.setTypeface(secondrayTypeFace);
temp_tag.setTypeface(secondrayTypeFace);
voltage_tag.setTypeface(secondrayTypeFace);
/** Code for Google Admob */
bLevel.setTypeface(myTypeface);
header.setTypeface(myTypeface);
// register the Broadcast Receiver
this.getActivity ().registerReceiver(this.batteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
// Setting up the Intent for the Power Usage details
usageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
// Check it for presence
resolveInfo = getActivity ().getPackageManager().resolveActivity(usageIntent, 0);
if (resolveInfo == null) {
//Toast.makeText.(this, "Not Support!", Toast.LENGTH_LONG).show();
cheakUsage.setEnabled(false);
} else {
cheakUsage.setEnabled(true);
}
// Setting up the functionality of the Button Clicks
cheakUsage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(usageIntent);
}
});
return root;
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
setUpStats(intent);
// Toast.makeText(getBaseContext(),
// ""+intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,-1),Toast.LENGTH_SHORT).show();
new ShowProgressClass().execute();
}
};
public void setUpStats(Intent intent) {
GetBatteryStats stats = new GetBatteryStats(intent);
health.setText("" + stats.getBatteryHealth());
tech.setText("" + stats.getBatteryTechnology());
temp.setText("" + stats.getBatteryTempreture());
voltage.setText("" + stats.getBatteryVoltage());
//Setting up typefaces for the stats
health.setTypeface(secondrayTypeFace);
tech.setTypeface(secondrayTypeFace);
temp.setTypeface(secondrayTypeFace);
voltage.setTypeface(secondrayTypeFace);
/**Save current voltage and level to calculate the Remaining time on Battery*/
SharedPreferences prefs=this.getActivity().getSharedPreferences(LEVEL, Context.MODE_PRIVATE);
prefs.edit().putString("level",""+level);
prefs.edit().putString("voltage",stats.getBatteryVoltage());
}
// For Upadating progress bar After loading
class ShowProgressClass extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < level; i++) {
publishProgress(1);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPreExecute() {
bProgress.setMax(100);
bProgress.setProgress(0);
// TODO Auto-generated method stub
}
@Override
protected void onProgressUpdate(Integer... values) {
bProgress.incrementProgressBy(values[0]);
bLevel.setText(" " + bProgress.getProgress() + "%");
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
此活动是关于我刚发现的手机电池信息... 我要编辑这个,但首先我需要它作为片段。
答案 0 :(得分:1)
发布的答案都是正确的,但我认为在Dictionary<string, ObservableCollection<Employee>> EmpList
内调用sortEmpName
的最佳做法
List<Department> DepList
答案 1 :(得分:0)
在android.widget
中使用root而不是getView() bLevel = (TextView) root.findViewById(R.id.blevel1);
bProgress = (ProgressBar) root.findViewById(R.id.progressBar);
cheakUsage = (ImageButton) root.findViewById(R.id.usage);
header = (TextView) root.findViewById(R.id.header);
tech = (TextView) root.findViewById(R.id.technology);
temp = (TextView) root.findViewById(R.id.tempreture);
voltage = (TextView) root.findViewById(R.id.voltage);
health = (TextView) root.findViewById(R.id.health);
答案 2 :(得分:0)
使用root.findViewById()
代替getView().findViewById()
。
因为您将root定义为oncreateview()
方法返回的视图。
答案 3 :(得分:0)
因为你没有在onCreateView方法中将视图返回给片段,所以在onCreateView方法中运行getView方法时,会导致NullPointerException。 你应该使用root.findViewById,而不是使用getView()。findViewById。