NullPointerException,片段布局中有一个按钮

时间:2014-02-24 12:59:05

标签: android android-fragments nullpointerexception onclicklistener imagebutton

已经解决了这个问题,在问题的最后看到了解决方案的代码示例,感谢您的回应!

我真的尝试过任何我能想到的东西,并从头到脚搜索这个网站。 我决定将我的布局分解为Fragments,并且在单击时无法找到改变ImageButton可见性的方法。

主要活动的代码如下(我修剪了与问题无关的任何内容):

public class PricingActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pricing);

    Log.i("camera", "declaring button");
    ImageButton camBt = (ImageButton)findViewById(R.id.button1);
    Log.i("camera", "button declared");

    camBt.setOnClickListener(listener);
}

ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        Log.i("camera", "onClick started");
        Camera.open().getParameters();
        Log.i("camera", "camera opened + get parameters");
        ImageButton camBt1 = (ImageButton)findViewById(R.id.button1);
        Log.i("camera", "camBt declared");
        camBt1.setVisibility(View.INVISIBLE);
        Log.i("camera", "visibility set to invisible");
    }
}; 
}

我在日志消息“按钮声明”后得到NullPointerException。 我尝试在onCreate内声明侦听器, 我尝试将侦听器声明为camBt.setOnClickListener行中的匿名内部类,  我尝试将按钮和监听器的整个声明移动到片段的类中。

当我尝试运行应用程序时累积了运行时错误,我甚至没有点击调用监听器的按钮。

当我禁用这些行时,它不会发生:

ImageButton camBt1 = (ImageButton)findViewById(R.id.button1);
camBt1.setVisibility(View.INVISIBLE);

有人可以帮我这个吗?

LogCat日志:

02-24 15:06:18.734: I/camera(20681): declaring button
02-24 15:06:18.734: I/camera(20681): button declared
02-24 15:06:18.784: D/AndroidRuntime(20681): Shutting down VM
02-24 15:06:18.784: W/dalvikvm(20681): threadid=1: thread exiting with uncaught exception (group=0x41ab6e10)
02-24 15:06:18.784: E/AndroidRuntime(20681): FATAL EXCEPTION: main
02-24 15:06:18.784: E/AndroidRuntime(20681): java.lang.RuntimeException: Unable to start activity ComponentInfo{il.co.bsl.b.s.lsoft/il.co.bsl.b.s.lsoft.PricingActivity}: java.lang.NullPointerException
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at     android.app.ActivityThread.access$700(ActivityThread.java:150)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.os.Looper.loop(Looper.java:137)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.app.ActivityThread.main(ActivityThread.java:5279)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at dalvik.system.NativeStart.main(Native Method)
02-24 15:06:18.784: E/AndroidRuntime(20681): Caused by: java.lang.NullPointerException
02-24 15:06:18.784: E/AndroidRuntime(20681):    at il.co.bsl.b.s.lsoft.PricingActivity.onCreate(PricingActivity.java:140)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.app.Activity.performCreate(Activity.java:5267)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-24 15:06:18.784: E/AndroidRuntime(20681):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-24 15:06:18.784: E/AndroidRuntime(20681):    ... 11 more

activity_pricing.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6687CEFF"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PricingActivity" >

<LinearLayout
    android:name="separator"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" >
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/topRightLayoutBlock"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/namefield_fragment"
            android:name="il.co.bsl.b.s.lsoft.NameField"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            tools:layout="@layout/name_field" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/topRightSeparator"
        android:name="separator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/topRightLayoutBlock"
        android:background="#000"
        android:orientation="horizontal" />


    <LinearLayout
        android:id="@+id/topMiddleLayoutBlock"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/topRightSeparator"
        android:layout_toRightOf="@+id/topLeftSeparator"
        android:orientation="vertical" >

    <fragment
        android:id="@+id/productionfield_fragment"
        android:name="il.co.bsl.b.s.lsoft.ProductionField"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:layout="@layout/production_field" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="254dp"
        android:layout_height="254dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
         >
    <fragment
        android:id="@+id/camera_fragment"
        android:name="il.co.bsl.b.s.lsoft.CameraFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/camera_fragment"
        />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/topLeftSeparator"
        android:name="separator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/frameLayout"
        android:background="#000"
        android:orientation="horizontal" />


</RelativeLayout>

<LinearLayout
    android:name="separator"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</RelativeLayout>

<LinearLayout
    android:name="separator"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</RelativeLayout>

<LinearLayout
    android:name="separator"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" >
</LinearLayout>

</LinearLayout>

这是片段的布局:

<?xml version="1.0" encoding="utf-8"?>
        <ImageButton
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        android:contentDescription="@string/photoButtonDescription"
        android:src="@drawable/cameraicon" />

解决问题的正确片段代码(我剪切了所有Log.i行以保持简单和可读):

public class CameraFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.camera_fragment, container, false);

    ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
    camBt.setOnClickListener(listener);

    return v;
}

ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{

@Override
public void onClick(View arg0)
{
    Camera.open().getParameters();
    arg0.setVisibility(View.INVISIBLE);
}
}; 
}

感谢您提供的精彩帮助!

3 个答案:

答案 0 :(得分:2)

camBt.setOnClickListener(new ImageButton.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        Log.i("camera", "onClick started");
        Camera.open().getParameters();
        Log.i("camera", "camera opened + get parameters");
        Log.i("camera", "camBt declared");
        ((ImageButton) arg0).setVisibility(View.INVISIBLE);
        Log.i("camera", "visibility set to invisible");
    }
};

编辑由于您使用的是片段,因此您应该以其他方式查找图片按钮:

ImageButton camBt = (ImageButton) getFragmentManager().findFragmentById(R.id.camera_fragment).getView().findViewById(R.id.button1);

答案 1 :(得分:0)

监听器在当时创建的活动时初始化,因为上下文引用不可用,因此您遇到NULLPointer异常

替换

ImageButton camBt1 = (ImageButton)findViewById(R.id.button1);

ImageButton camBt1 =(ImageButton)arg0;
  

在ImageButton.OnClickListener侦听器

答案 2 :(得分:0)

在加载GUI元素之前,

OnCreateView被称为,导致空指针异常。

覆盖onActivityCreated并将代码放在那里。这解决了我的问题。

@Override
public void onActivityCreated(Bundle savedInstanceType) {
    super.onActivityCreated(savedInstanceType);
    //do custom init here
}