-1

Eclipse finds no errors in my code, but when I try to run the app on an emulator, it opens and then immediately crashes. Logcat gives me the vague nullPointerException error. I can comment out the onKeyDown method, and then it runs just fine. But of course, I can't use the back key to go back, it will just close out the app.

My code is as follows:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
static String[] items = {"Campaign", "Multiplayer", "Zombies"};

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

        ListView listView = (ListView) findViewById(R.id.listview);
        final ViewFlipper viewflipper = (ViewFlipper)   findViewById(R.id.viewflipper1);
        listView.setAdapter(new BaseAdapter(){
            public int getCount() {
            return items.length;
            }
            public Object getItem(int position) {
            return items[position];
            }
            public long getItemId(int position) {
            return position;
            }
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.list_row, null);
                TextView textView = (TextView) view.findViewById(R.id.TextView01);
                textView.setText(items[position]);
                textView.setTextColor(Color.rgb(255,106,0));
                textView.setTextSize(24);
                TextView textView1 = (TextView) findViewById(R.id.textView1);
                textView1.setTextColor(Color.rgb(255,106,0));
            return view;
            }});

            listView.setOnItemClickListener(new OnItemClickListener(){
              public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                 if (position == 0) {
                     viewflipper.showNext();
                 }if (position == 1) {
                     viewflipper.showNext();
                     viewflipper.showNext();
                 }if (position == 2) {
                     viewflipper.showNext();
                     viewflipper.showNext();
                     viewflipper.showNext();
                 }}   

        });    

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;  
    }

    ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
             if(viewflipper.getDisplayedChild() == 0){
                 viewflipper.showPrevious();
              }if(viewflipper.getDisplayedChild() == 1){
                 viewflipper.showPrevious();
                 viewflipper.showPrevious();
              }if(viewflipper.getDisplayedChild() == 2){
                 viewflipper.showPrevious();
                 viewflipper.showPrevious();
                 viewflipper.showPrevious();
              }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

};

My Logcat is as follows:

01-21 11:38:46.151: E/AndroidRuntime(760): FATAL EXCEPTION: main

01-21 11:38:46.151: E/AndroidRuntime(760): java.lang.RuntimeException: Unable to instantiate activity 
ComponentInfo{com.example.blackopsiiexperience/com.example.blackopsiiexperience.MainActivity}: java.lang.NullPointerException

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread.access$600(ActivityThread.java:141)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.os.Handler.dispatchMessage(Handler.java:99)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.os.Looper.loop(Looper.java:137)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread.main(ActivityThread.java:5039)

01-21 11:38:46.151: E/AndroidRuntime(760):  at java.lang.reflect.Method.invokeNative(Native Method)

01-21 11:38:46.151: E/AndroidRuntime(760):  at java.lang.reflect.Method.invoke(Method.java:511)

01-21 11:38:46.151: E/AndroidRuntime(760):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

01-21 11:38:46.151: E/AndroidRuntime(760):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

01-21 11:38:46.151: E/AndroidRuntime(760):  at dalvik.system.NativeStart.main(Native Method)

01-21 11:38:46.151: E/AndroidRuntime(760): Caused by: java.lang.NullPointerException

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.Activity.findViewById(Activity.java:1839)

01-21 11:38:46.151: E/AndroidRuntime(760):  at com.example.blackopsiiexperience.MainActivity.<init>(MainActivity.java:73)

01-21 11:38:46.151: E/AndroidRuntime(760):  at java.lang.Class.newInstanceImpl(Native Method)

01-21 11:38:46.151: E/AndroidRuntime(760):  at java.lang.Class.newInstance(Class.java:1319)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)

01-21 11:38:46.151: E/AndroidRuntime(760):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

01-21 11:38:46.151: E/AndroidRuntime(760):  ... 11 more


Any suggestions?
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Eclipse can't find RuntimeExceptions, because they occur at runtime. And a NullPointerException isn't vague at all. It tells you that something that shouldn't be null is null. Also, it gives you a line number. – keyser Jan 21 '13 at 18:57

1 Answers1

6

move

ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper1);

inside onCreate of Activity after setting layout for Activity

EDIT :

declare Viewflipper as class level field to access it through out class instead of method level as :

public class MainActivity extends Activity {
static String[] items = {"Campaign", "Multiplayer", "Zombies"};
 ListView listView;
 ViewFlipper viewflipper;  //<<<< declare here
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listview);
        viewflipper = (ViewFlipper)   findViewById(R.id.viewflipper1);
        //your code here.....
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I tried that but now I get the error 'Cannot refer to a non-final variable viewflipper inside an inner class defined in a different method' on lines 51, 53, 54, 56, 57, and 58. Along with the error 'viewflipper cannot be resolved' on lines 73-81. – Blackhawk497 Jan 21 '13 at 20:06
  • @Blackhawk497 : just declare ViewFlipper viewflipper; at class level means before onCreate of Activity and initialized it inside onCreate method after setContentView – ρяσѕρєя K Jan 21 '13 at 20:08
  • 1
    @Blackhawk497 I recommend that you learn Java before you try to develop Android apps and use Google some more. If you google for that error, you will get more than enough information to resolve it. – Simon Jan 21 '13 at 20:09
  • I fixed it, but I don't have a reputation above 15 to post a solution to my own problem, not at least for another 6 hours. So until then, you'll just have to wait to see what I did. – Blackhawk497 Jan 21 '13 at 20:17
  • @Blackhawk497 : nice and great can i known what u have done to solve it . and next time before asking question u must read whole [FAQ SECTION](http://stackoverflow.com/faq) because i give u answer as exectly what is current issue in your code according to your code and log posted with question .Thanks – ρяσѕρєя K Jan 21 '13 at 20:24