0

I'm working on a piece of code that could switch image using Menu inflater, i've worked on code but it gives me force close error as soon as i click the button. here is my code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.cameraa:
            Toast.makeText(this, "Start Camera!", Toast.LENGTH_LONG).show();
            break;

        case R.id.gallery:
         Toast.makeText(this, "this is gallery!", Toast.LENGTH_LONG).show();
        break;


    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {


    case R.id.ghost_gallery:
            switcher.showNext();
break
            }
            return true;
        }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 <ViewSwitcher
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:scaleType="matrix"
            android:src="@drawable/banana" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:scaleType="matrix"
            android:src="@drawable/apple" />
        </FrameLayout>
    </ViewSwitcher>

here is my log

05-21 03:19:35.733: E/AndroidRuntime(758): FATAL EXCEPTION: main
05-21 03:19:35.733: E/AndroidRuntime(758): java.lang.NullPointerException
05-21 03:19:35.733: E/AndroidRuntime(758):  at org.example.touch.Touch.nextView(Touch.java:188)
05-21 03:19:35.733: E/AndroidRuntime(758):  at org.example.touch.Touch.onOptionsItemSelected(Touch.java:180)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.app.Activity.onMenuItemSelected(Activity.java:2195)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.view.View$PerformClick.run(View.java:8816)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.os.Handler.handleCallback(Handler.java:587)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.os.Looper.loop(Looper.java:123)
05-21 03:19:35.733: E/AndroidRuntime(758):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 03:19:35.733: E/AndroidRuntime(758):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 03:19:35.733: E/AndroidRuntime(758):  at java.lang.reflect.Method.invoke(Method.java:521)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 03:19:35.733: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 03:19:35.733: E/AndroidRuntime(758):  at dalvik.system.NativeStart.main(Native Method)

Kindly help me regarding this matter.... Best Regards,

Numair
  • 1,062
  • 1
  • 20
  • 41

4 Answers4

1

Well, that's easy: switcher is null. Is to be expected since it doesn't even has an id in the layout.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

Did you initialize switcher in onCreate()?

You need to give it an id in the layout:

android:id="@+id/switcher_view"

Declare ViewSwitcher outside any methods as switcher:

private ViewSwitcher switcher;

Then initialize it like this (in onCreate()):

switcher = (ViewSwitcher) findViewById(R.id.switcher_view);
Dirk Jäckel
  • 2,979
  • 3
  • 29
  • 47
  • yup.. i've initialized it too – Numair May 20 '12 at 21:52
  • And its still throwing an exception at the same line in your code? Or do you mean it was initialized all along? How did you do that while ViewSwitcher did not have an id? – Dirk Jäckel May 20 '12 at 21:56
  • i've used Toast message to verify its working, then apply image switcher to it...it gives me force close error – Numair May 20 '12 at 22:01
  • Are you sure there is no stack trace visible in logcat when the force close occurs? – Dirk Jäckel May 21 '12 at 07:14
  • I am sorry. My answer was misleading. If you have declared switcher as a member variable of the class you must not declare it again in onCreate(). I have corrected my answer. – Dirk Jäckel May 21 '12 at 09:28
  • Jackel i'm new to android... and would apreciate if you can help me how to acheive the task..i've edited my ans...the other two cases are working perfectly..but the third one gives me error.... – Numair May 21 '12 at 21:45
0

You didn't give your ViewSwitcher an ID. And also you need to use findViewById to refer to it in your other code.

reconditesea
  • 146
  • 5
0

i used

private void showImage() {

        Toast.makeText(Touch.this, " User don't  ", Toast.LENGTH_SHORT);
        ImageView imgView = (ImageView) findViewById(R.id.imageView);
        imgView.setImageResource(mImageIds[image_index]);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case (R.id.previous_btn):

            image_index--;

            if (image_index == -1) {
                image_index = MAX_IMAGE_COUNT - 1;
            }

            showImage();
            break;
}}

which works pretty well for me...

Numair
  • 1,062
  • 1
  • 20
  • 41