我处理片段时遇到问题。
我在活动1中。我将片段添加到此活动并调用方法。 然后这个方法在片段类中调用其他metod。 在这个方法中,我想要button.setVisibility(View.VISIBLE),但它会抛出NullPointer。
代码:
拥有类容器:
public class Container
{
private ImageButton ib;
private int stan;
private int wartosc;
public Container(ImageButton ib, int stan, int wartosc)
{
this.ib = ib;
this.stan = stan;
this.wartosc = wartosc;
}
public int getStan()
{
return stan;
}
public int getWartosc()
{
return wartosc;
}
public void setWartosc(int wartosc)
{
this.wartosc = wartosc;
}
public void setStan(int stan)
{
this.stan = stan;
}
public ImageButton getButton()
{
return ib;
}
}
片段:
ImageButton b1;
Container button1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
V = inflater.inflate(R.layout.face_fragment, container, false);
b1 = (ImageButton) getActivity().findViewById(R.id.nr1);
// i also tried something like that:
b1 = (ImageButton) V.findViewById(R.id.nr1);
context = V.getContext();
addResources();
button1 = new Container(b1, 1, 0);
return V;
}
抛出nullpointer的方法的片段:
public void setVisible()
{
if(all)
{
button1.getButton().setVisibility(View.VISIBLE); //here nullpointer
请帮助我,我不知道......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
tools:context=".FaceFragment"
android:weightSum="20"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="10"
android:weightSum="30"
android:layout_width="fill_parent"
android:layout_height="0dp" >
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button1Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button2Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button3Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
</LinearLayout>
<LinearLayout
android:layout_weight="10"
android:weightSum="30"
android:layout_width="fill_parent"
android:layout_height="0dp">
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr4"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button4Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button5Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/nr6"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="10"
android:adjustViewBounds="true"
android:background="@null"
android:onClick="button6Click"
android:scaleType="fitCenter"
android:src="@drawable/domyslnie" />
</LinearLayout>
</LinearLayout>
logcat的:
11-06 18:58:20.289: E/AndroidRuntime(24368): FATAL EXCEPTION: main
11-06 18:58:20.289: E/AndroidRuntime(24368): java.lang.IllegalStateException: Could not execute method of the activity
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.view.View$1.onClick(View.java:3660)
1-06 18:58:20.289: E/AndroidRuntime(24368): at android.view.View.performClick(View.java:4162)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.view.View$PerformClick.run(View.java:17152)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.os.Handler.handleCallback(Handler.java:615)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.os.Looper.loop(Looper.java:137)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.app.ActivityThread.main(ActivityThread.java:4867)
11-06 18:58:20.289: E/AndroidRuntime(24368): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 18:58:20.289: E/AndroidRuntime(24368): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 18:58:20.289: E/AndroidRuntime(24368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-06 18:58:20.289: E/AndroidRuntime(24368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-06 18:58:20.289: E/AndroidRuntime(24368): at dalvik.system.NativeStart.main(Native Method)
11-06 18:58:20.289: E/AndroidRuntime(24368): Caused by: java.lang.reflect.InvocationTargetException
11-06 18:58:20.289: E/AndroidRuntime(24368): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 18:58:20.289: E/AndroidRuntime(24368): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 18:58:20.289: E/AndroidRuntime(24368): at android.view.View$1.onClick(View.java:3655)
11-06 18:58:20.289: E/AndroidRuntime(24368): ... 11 more
11-06 18:58:20.289: E/AndroidRuntime(24368): Caused by: java.lang.NullPointerException
11-06 18:58:20.289: E/AndroidRuntime(24368): at pl.brysp.game.speedsquare.FaceFragment.setVisible(FaceFragment.java:189)
11-06 18:58:20.289: E/AndroidRuntime(24368): at pl.brysp.game.speedsquare.FaceFragment.setRemember(FaceFragment.java:142)
11-06 18:58:20.289: E/AndroidRuntime(24368): at pl.brysp.game.speedsquare.Game.start(Game.java:63)
11-06 18:58:20.289: E/AndroidRuntime(24368): ... 14 more
答案 0 :(得分:2)
getActivity().findViewById
无法在onCreateView
上运行,因为视图尚未附加。
使用V.findViewById
代替