我的应用程序在所有设备(avd 2.3.3,avd 4.4.2,avd 4.1.2 ecc ..)和真实设备(如Android 4.1 Android 2.3.3 - 2.3.7 Android 4.0.3 - 4.0)上运行完美.4 Android 4.2 Android 4.3 Android 4.4 Android 2.2 Android 3.2 Android 2.1 Android 3.1但它仅在谷歌Nexus 7 4.4.2上给我以下错误
04-15 15:50:16.676: E/AndroidRuntime(24520): FATAL EXCEPTION: main
04-15 15:50:16.676: E/AndroidRuntime(24520): Process: dado.auto3gdataswitch, PID: 24520
04-15 15:50:16.676: E/AndroidRuntime(24520): java.lang.RuntimeException: Unable to resume activity {dado.auto3gdataswitch/dado.auto3gdataswitch.MainActivity}: java.lang.NullPointerException
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.os.Looper.loop(Looper.java:136)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 15:50:16.676: E/AndroidRuntime(24520): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 15:50:16.676: E/AndroidRuntime(24520): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 15:50:16.676: E/AndroidRuntime(24520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 15:50:16.676: E/AndroidRuntime(24520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 15:50:16.676: E/AndroidRuntime(24520): at dalvik.system.NativeStart.main(Native Method)
04-15 15:50:16.676: E/AndroidRuntime(24520): Caused by: java.lang.NullPointerException
04-15 15:50:16.676: E/AndroidRuntime(24520): at dado.auto3gdataswitch.MainActivity.onResume(MainActivity.java:112)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.Activity.performResume(Activity.java:5310)
04-15 15:50:16.676: E/AndroidRuntime(24520): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
04-15 15:50:16.676: E/AndroidRuntime(24520): ... 12 more
我的活动:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final PackageManager pm = getPackageManager();
final ComponentName compName = new ComponentName(getApplicationContext(),NetworkReceiver.class);
final Button button = (Button) findViewById(R.id.button1);
final ConnectivityManager conman = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
final WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
int isEnabled = pm.getComponentEnabledSetting(compName);
if (isEnabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Toast.makeText(MainActivity.this, R.string.broadcastStop, Toast.LENGTH_SHORT).show();
button.setText(R.string.btEnable);
}
else {
wifiManager.setWifiEnabled(true);
Intent i = new Intent(getBaseContext(), MainService.class);
startService(i);
Toast.makeText(MainActivity.this, R.string.broadcastRun, Toast.LENGTH_SHORT).show();
button.setText(R.string.btDisable);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int isEnabled = pm.getComponentEnabledSetting(compName);
if (isEnabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
wifiManager.setWifiEnabled(true);
Intent i = new Intent(getBaseContext(), MainService.class);
startService(i);
pm.setComponentEnabledSetting(compName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
Toast.makeText(MainActivity.this, R.string.broadcastRun, Toast.LENGTH_SHORT).show();
button.setText(R.string.btDisable);
}
else {
pm.setComponentEnabledSetting(compName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
Toast.makeText(MainActivity.this, R.string.broadcastStop, Toast.LENGTH_SHORT).show();
button.setText(R.string.btEnable);
}
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
final TextView vibration = (TextView) findViewById(R.id.getVibro);
final TextView sound = (TextView) findViewById(R.id.getSound);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean(PreferencesActivity.SOUND, false)) {
sound.setText(getString(R.string.soundOn));
}
else {
sound.setText("BLABLA");<<ERROR HERE
}
if (prefs.getBoolean(PreferencesActivity.VIBRO, false)) {
vibration.setText(getString(R.string.vibroOn));
}
else {
vibration.setText(getString(R.string.vibroOff));
}
}
解决了!我为手机和平板电脑使用了三种不同的布局,我忘了改变平板电脑的布局! TextView不存在!
答案 0 :(得分:0)
这一行:
sound.setText(R.string.soundOff);
应该是:
sound.setText(getString(R.string.soundOff));
编辑:下面几行..您可以在logcat中看到nullpointer发生的位置,转到该行,看看那里可能出现的问题
编辑2:此行告诉您问题所在:
Caused by: java.lang.NullPointerException
at dado.auto3gdataswitch.MainActivity.onResume(MainActivity.java:113)
答案 1 :(得分:0)
尝试不使用修饰符,即
TextView sound = (TextView) findViewById(R.id.getSound)
同时检查身份getSound
的文字视图是否在main.xml
。
如果错误仍然存在,请执行以下更改 -
从onResume()
删除以下行,并在onCreate()
setContentView(R.layout.main);
放入
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView vibration = (TextView) findViewById(R.id.getVibro);
final TextView sound = (TextView) findViewById(R.id.getSound);
//Rest of the code
}