Hi everybody I'm struggling a strange problem. I changed my ViewFlipper to ViewPager and since there my App crashed. I found out that it's not the Viewpager that crashes but the button.setOnClickListener method. I commented out everything else so I noticed that the App works fine if I'm commenting out my two Buttons with that method. And it's not the thing inside onClick(), I treied it out and it crashes even if it's empty.but here is some code of the one im fighting with:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




    MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager)findViewById(R.id.pager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);
    new Toolbox();    

push = (Button)findViewById(R.id.pushbutton);
             push.setOnClickListener(new OnClickListener() {


                public void onClick(View v) {
                    head=head_field.getText().toString();
                    note=note_field.getText().toString();
                    try {
                        Toolbox.createTask(userToken,task, Dashboard_id, head, note, service);
                        head_field.setText("");
                        note_field.setText("");
                    } catch (ProducteevException e) {
                        return;

                    }
                }
            });

this is the button xml excerpt:

<Button
             android:id="@+id/pushbutton"
             android:layout_width="200dp"
             android:layout_height="200dp"
             android:layout_gravity="center_horizontal"
             android:layout_marginTop="35dp"
             android:background="@drawable/push_button" />

and here the LogCat

01-12 20:50:24.402: W/dalvikvm(28405): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-12 20:50:24.402: E/AndroidRuntime(28405): FATAL EXCEPTION: main
01-12 20:50:24.402: E/AndroidRuntime(28405): java.lang.RuntimeException: Unable to start activity ComponentInfo{producteev.push/producteev.push.Producteev_pushActivity}: java.lang.NullPointerException
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.os.Looper.loop(Looper.java:123)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at java.lang.reflect.Method.invoke(Method.java:507)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at dalvik.system.NativeStart.main(Native Method)
01-12 20:50:24.402: E/AndroidRuntime(28405): Caused by: java.lang.NullPointerException
01-12 20:50:24.402: E/AndroidRuntime(28405):    at producteev.push.Producteev_pushActivity.onCreate(Producteev_pushActivity.java:122)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-12 20:50:24.402: E/AndroidRuntime(28405):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-12 20:50:24.402: E/AndroidRuntime(28405):    ... 11 more

Would be amazing if somebody knows that Problem.

有帮助吗?

解决方案

Make sure your push buton id is corect, it seems that your findViewById() returns null...

其他提示

Since I can't see the entirety of your code, it's hard to know for sure, but it appears that the error may be getting thrown upon creation, from your stacktrace. Since nothing happens within the onClick() method, the only other thing I can think of is the fact that push must be null, from the code you have posted. This may be caused, if that is the issue, by either the ID being incorrect (the one being passed into the findViewById() call), or, if the compiler seems ok with the id you're passing in (I think it wouldn't compile otherwise) it may be the casting with (Button) that is not working properly. I would check the type of your R.id.pushbutton's actual class type and make sure that it is a (Button).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top