首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fragment setVisibility NullPointer

fragment setVisibility NullPointer
EN

Stack Overflow用户
提问于 2014-11-07 01:38:32
回答 1查看 335关注 0票数 0

我在处理碎片时遇到了问题。

我在活动1中。我将片段添加到此活动中,并调用方法。然后此方法调用其他方法,但在fragment类中。在这个方法中,我想button.setVisibility(View.VISIBLE),但它抛出了NullPointer。

代码:

自己的类容器:

代码语言:javascript
复制
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;
    }
}

片段:

代码语言:javascript
复制
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;
}

以及抛出空指针的方法片段:

代码语言:javascript
复制
public void setVisible()
{       
    if(all)
    {
        button1.getButton().setVisibility(View.VISIBLE); //here nullpointer

求求你帮帮我我不知道..。

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-11-07 01:43:08

getActivity().findViewById将不能在onCreateView上工作,因为视图尚未附加。

请改用V.findViewById

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26785843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档