-2

The thing I'm trying to implement is a OnClick for my ListView so when I click in my ListView in tab1, I want to switch tabs to tab2 and then I want to save the clicked ListView row to be displayed in same ListView in tab2 but with more information. But I'm getting this error.

Why am I getting this error, and how can I display the chosen row in tab1 to tab2?

public class Tab1 extends TabActivity {

    WifiManager wifiManager;
    WifiScanReceiver wifiReciever;
    String ssid;
    int channel;
    int level;
    ArrayAdapter adapter;
    ListView list;
    ArrayList<String> wifis;
    WifiInfo wifiInfo;
    TabHost tabHost = null;

    // TabSpec Names
    private static final String TAB1 = "Tab1";
    private static final String TAB2 = "Tab2";
    private static final String TAB3 = "Tab3";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1);

        list = (ListView) findViewById(R.id.text);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        wifiInfo = wifiManager.getConnectionInfo();

        wifis = new ArrayList<String>(); //initialize wifis
        wifis.add("loading...");
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, wifis);
        list.setAdapter(adapter);

        wifiManager.startScan(); //make sure this is the last call
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                v.findViewById(R.id.text);
                switchTab(1);
            }
        }); };

    public void switchTabs(int tab) {
        tabHost.setCurrentTab(tab);
    }

    public void switchTab(int tab) {
        tabHost.setCurrentTab(tab);
    }

    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    public static int convertFrequencyToChannel(int freq) {
        if (freq >= 2412 && freq <= 2484) {
            return (freq - 2412) / 5 + 1;
        } else if (freq >= 5170 && freq <= 5825) {
            return (freq - 5170) / 5 + 34;
        } else {
            return -1;
        }
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }


    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            wifis.clear(); //add this
            for (int i = 0; i < wifiScanList.size(); i++) {
                ssid = wifiScanList.get(i).SSID; //Get the SSID
                level = wifiScanList.get(i).level;
                channel = wifiScanList.get(i).frequency;

                //use add here:
                wifis.add(ssid + " " + level + " dBm "); //append to the other data
                wifis.toString();
            }

            adapter.notifyDataSetChanged(); //add this
            wifiManager.startScan(); //start a new scan to update values faster

        }
    }

}

Error messages

05-20 10:40:42.710    4694-4694/com.example.isac.tabtry W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41477930)
05-20 10:40:42.718    4694-4694/com.example.isac.tabtry E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException

       at com.example.isac.tabtry.Tab1.switchTab(Tab1.java:73)
        at com.example.isac.tabtry.Tab1$1.onItemClick(Tab1.java:64)
        at android.widget.AdapterView.performItemClick(AdapterView.java:301)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1508)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3293)
        at android.widget.AbsListView$1.run(AbsListView.java:4554)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
Isac
  • 145
  • 3
  • 12
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – 2Dee May 20 '15 at 08:46
  • ive read the article and i understand what he is saying, thanks. But where is the variable with no pointer in my code? – Isac May 20 '15 at 08:55
  • 1
    Post full stack trace of exception – Prashant May 20 '15 at 08:57

2 Answers2

1

You are not initializing the tabHost object in your whole code, as this object is not initialized your code :

 public void switchTab(int tab) {
        tabHost.setCurrentTab(tab); // Throwing exception here
    }

tabHost = null here. Set it first.

Ed George
  • 5,082
  • 2
  • 37
  • 61
Prashant
  • 4,244
  • 5
  • 32
  • 74
  • i now initialized tabHost with null now but error is still there – Isac May 20 '15 at 09:23
  • I have not suggested you to set it to null. Initialize it to some value like : tabHost = new TabHost(this); something like this. It should not be 'null' otherwise it will throw NPE. – Prashant May 20 '15 at 09:26
  • ,this works thanks, but now i cant change currenttab :S – Isac May 20 '15 at 09:43
  • I think you are using TabHost for first time search google for some tutorials, here one i found : http://www.mkyong.com/android/android-tablayout-example/ – Prashant May 20 '15 at 09:46
  • thanks man, ive read it now. But in my onclick i have tabHost.setCurrentTab(1); but when i click it doesnt change. – Isac May 20 '15 at 09:59
0

Please have a look at these two tutorial:

http://developer.android.com/reference/android/app/TabActivity.html

http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html

Just a add on about basic debugging in case you don't know.

In future, you may faced over thousand lines of code and finding the causes of exceptions is getting harder. Therefore, learning how to debug is very important.

Some introduction of debugging can be found here:

https://developer.android.com/tools/debugging/debugging-studio.html

Detailed explaination: https://www.jetbrains.com/idea/help/debugging.html

Some brief description:

  1. Add breakpoint to the problem/buggy or any part

  2. F5 to enter debug mode

  3. Check the value (for your case : nullpointerexception you may check which value is null , which is mostly tabhost since you didnt initialized it)

You can also use the threads tab to check for multithreading issues in future too.

Moustach
  • 2,888
  • 1
  • 12
  • 23
pengMiao
  • 314
  • 1
  • 6