尝试使用ImageViewer时出现NullPointerException

时间:2014-11-14 09:30:53

标签: android android-fragments nullpointerexception android-imageview

所以我正在尝试制作一个石头剪刀游戏,它工作正常,直到我想添加碎片。我不得不改变很多事情,现在我无法让这件事情发挥作用。这很难解释,所以我只会告诉你重要的代码。我对整体编程非常陌生。

Fragmenttwo

private static Fragment theFragment;

private void clickListener(choice mychoice){
    hideButtons();

    choice compchoice = choice.choices.getRandom();
    //int outcome = choice.decideOutcome(mychoice, compchoice);

    compchoice.getImage(theFragment, compchoice);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
     final View view = inflater.inflate(R.layout.start_board, container, false);

                Button rbutton = (Button) view.findViewById(R.id.buttonRock); 

                rbutton.setOnClickListener(new View.OnClickListener(){
                  public void onClick(View v) {
                    view.setBackgroundResource(R.drawable.rockdone);

                    choice mychoice = new choice(choices.ROCK);

                    clickListener(mychoice);
                }              
            }); 
     return view;
 }

选择课

private static ActionBarActivity theActivity;

public static enum choices {
    ROCK,
    SCISSOR,
    PAPER,

    public static choice getRandom() {
        return new choice(values()[(int) (Math.random() * values().length)]);
    }
}

public View getImage(Fragment a, choice compchoice){
    ImageView image = (ImageView) theActivity.findViewById(R.id.imageViewer);
    image.setImageResource(R.drawable.rockcom);
    image.setVisibility(View.VISIBLE);
  return null;
}

start_board.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/start_board"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rps9"
android:visibility="visible" >

  <Button
    android:id="@+id/buttonRock"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp"
    android:text="@string/brock"
    android:textColorLink="@android:color/transparent"
    android:visibility="visible" />

  <ImageView
    android:id="@+id/imageViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/rockcom"
    android:visibility="gone"/>

</RelativeLayout>

logcat的

11-14 08:57:20.964: E/AndroidRuntime(2066): FATAL EXCEPTION: main
11-14 08:57:20.964: E/AndroidRuntime(2066): Process: com.gerfort.gerfortrps, PID: 2066
11-14 08:57:20.964: E/AndroidRuntime(2066): java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.app.ActionBarActivity.findViewById(int)' on a null object reference
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.gerfort.gerfortrps.choice.getImage(choice.java:39)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.gerfort.gerfortrps.Fragmenttwo.clickListener(Fragmenttwo.java:108)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.gerfort.gerfortrps.Fragmenttwo.access$2(Fragmenttwo.java:101)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.gerfort.gerfortrps.Fragmenttwo$4.onClick(Fragmenttwo.java:165)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at android.view.View.performClick(View.java:4756)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at     android.view.View$PerformClick.run(View.java:19749)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at android.os.Handler.handleCallback(Handler.java:739)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at android.os.Looper.loop(Looper.java:135)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at java.lang.reflect.Method.invoke(Native Method)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at java.lang.reflect.Method.invoke(Method.java:372)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-14 08:57:20.964: E/AndroidRuntime(2066):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果我忘记了任何密码,请通知我。

2 个答案:

答案 0 :(得分:1)

ImageView而不是片段传递给方法 getImage

改变这个:

compchoice.getImage(theFragment, compchoice);

致:

compchoice.getImage(image, compchoice);

并将onCreateView中的ImageView初始化为:

image = (ImageView) view.findViewById(R.id.imageViewer);

新方法签名将是:

public void getImage(ImageView image, choice compchoice)

代码将是:

public void getImage(ImageView image, choice compchoice){

    image.setImageResource(R.drawable.rockcom);
    image.setVisibility(View.VISIBLE);
}

答案 1 :(得分:0)

您必须简单地将imageview添加为

ImageView iv = (ImageView) view.findViewById(R.id.imageViewer);

多数民众赞成......