【问题标题】:Android Image not switchingAndroid图像不切换
【发布时间】:2012-05-20 21:26:56
【问题描述】:

我正在编写一段可以使用 Menu inflater 切换图像的代码,我已经编写过代码,但是一旦我单击按钮,它就会给我强制关闭错误。这是我的代码:

@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>

这是我的日志

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)

请帮我解决这个问题.... 最好的问候,

【问题讨论】:

  • @Simon 请找到我的 logcat..我已经编辑了我的答案
  • 切换器未初始化,仍为空。
  • 我正在使用另外两个案例,它们工作得很好,当我点击那个按钮时只会给我强制关闭错误
  • 你能把你声明变量切换器的代码贴出来吗?
  • @reconditesea 我已经解决了我的问题..谢谢你的时间

标签: android menu imageswitcher


【解决方案1】:

嗯,这很简单:switchernull。这是意料之中的,因为它甚至在布局中都没有 id。

【讨论】:

    【解决方案2】:

    你有没有在 onCreate() 中初始化切换器?

    你需要在布局中给它一个id:

    android:id="@+id/switcher_view"
    

    将任何方法之外的 ViewSwitcher 声明为切换器:

    private ViewSwitcher switcher;
    

    然后像这样初始化它(在 onCreate() 中):

    switcher = (ViewSwitcher) findViewById(R.id.switcher_view);
    

    【讨论】:

    • 是的..我也初始化了它
    • 它仍然在你的代码的同一行抛出异常?或者你的意思是它一直被初始化?在 ViewSwitcher 没有 id 的情况下你是怎么做到的?
    • 我使用 Toast 消息来验证它的工作,然后应用图像切换器...它给了我强制关闭错误
    • 当强制关闭发生时,您确定在 logcat 中没有可见的堆栈跟踪吗?
    • 对不起。我的回答具有误导性。如果已将 switcher 声明为类的成员变量,则不得在 onCreate() 中再次声明它。我已经更正了我的答案。
    【解决方案3】:

    你没有给你的ViewSwitcher 一个ID。您还需要使用findViewById 在您的其他代码中引用它。

    【讨论】:

      【解决方案4】:

      我用过

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

      这对我来说效果很好......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-11
        • 2013-06-20
        • 1970-01-01
        • 2020-09-05
        相关资源
        最近更新 更多