无法仅在关系7 4.4.2上恢复活动



我的应用程序在所有设备(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,但它只在带有 4.4.2的Google Nexus 7上给我以下错误

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不存在!

此行:

  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)

只需尝试不使用修饰符final,即

TextView sound = (TextView) findViewById(R.id.getSound)

还要检查id为getSound的文本视图是否在main.xml中。

如果错误仍然存在,请执行以下更改-

onResume()中删除以下行,并将它们放在setContentView(R.layout.main); 之后的onCreate()

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
}

相关内容

  • 没有找到相关文章

最新更新