-1

My app runs perfectly on all devices (avd 2.3.3, avd 4.4.2, avd 4.1.2 ecc..) and real devices such as 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 BUT IT give me the following error only on Google Nexus 7 with 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

My activity:

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));
    }
}

Solved! I use three different layouts for phones and tablets and I had forgotten to change the layout of the tablet! The TextView were not present!

Cœur
  • 37,241
  • 25
  • 195
  • 267
TecnoHelp83
  • 59
  • 1
  • 8

2 Answers2

0

this line:

  sound.setText(R.string.soundOff);

should be:

  sound.setText(getString(R.string.soundOff));

Edit: same a few rows below.. You can see where the nullpointer happens in your logcat, go to that line and see what could be wrong there

Edit 2: this line tells you where the problem is:

   Caused by: java.lang.NullPointerException
    at dado.auto3gdataswitch.MainActivity.onResume(MainActivity.java:113)
Pontus Backlund
  • 1,017
  • 1
  • 10
  • 17
0

Just try without a modifier final i.e.

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

Also check if the textview with id getSound is in main.xml.

If error persists, do the following change- -

Remove the following lines from onResume() and put them onCreate() after 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
}
sjain
  • 23,126
  • 28
  • 107
  • 185