在Fragment中处理多个ImageButton操作

时间:2017-03-15 17:35:53

标签: android android-fragments

我是Android编码的初学者。我使用BottomNavigationBar活动模板在Android studio中创建了简单的应用程序。

问题是,我想在一个片段中有两个ImageButtons,它将打开具有特定电话号码的电话拨号器。我在SO上找到了一些答案,但它们没有用。

public class More extends Fragment implements View.OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ((MainActivity) getActivity())
            .setActionBarTitle("Kontakt");

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_more, container, false);
}

public More() {
    sib1.setOnClickListener(this);
    imageButton3.setOnClickListener(this);
}

ImageButton sib1 = (ImageButton) getView().findViewById(R.id.sib1);
ImageButton imageButton3 = (ImageButton) getView().findViewById(R.id.imageButton3);

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.sib1:
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:0424321268"));
            startActivity(intent);
            break;

        case R.id.imageButton3:
            Intent intent2 = new Intent(Intent.ACTION_DIAL);
            intent2.setData(Uri.parse("tel:other_number"));
            startActivity(intent2);
            break;
}
}
}

fragment_more.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragmentPrimary"
tools:context="sk.spspb.android.More">


<ImageButton
    android:id="@+id/sib1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/call"
    android:layout_marginTop="24dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onClick" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/sib1"
    android:layout_toEndOf="@+id/sib1"
    android:layout_toRightOf="@+id/sib1"
    android:text="Sekretariát"
    android:textColor="@color/textMain"
    android:textSize="18sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_toEndOf="@+id/sib1"
    android:layout_toRightOf="@+id/sib1"
    android:text="042 / 432 12 68"
    android:textColor="@color/colorAccent" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/call"
    android:layout_below="@+id/sib1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Riaditeľ školy"
    android:textColor="@color/textMain"
    android:textSize="18sp"
    android:layout_alignTop="@+id/imageButton3"
    android:layout_toRightOf="@+id/imageButton3"
    android:layout_toEndOf="@+id/imageButton3" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView5"
    android:layout_toEndOf="@+id/imageButton3"
    android:layout_toRightOf="@+id/imageButton3"
    android:text="042 / 432 30 52"
    android:textColor="@color/colorAccent" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="(Otvorí sa aplikácia na volanie)"
    android:layout_below="@+id/textView3"
    android:layout_toRightOf="@+id/sib1"
    android:layout_toEndOf="@+id/sib1" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="(Otvorí sa aplikácia na volanie)"
    android:layout_below="@+id/textView6"
    android:layout_toRightOf="@+id/imageButton3"
    android:layout_toEndOf="@+id/imageButton3" />

和logcat输出:

03-15 18:24:33.708 29236-29236/sk.spspb.android E/AndroidRuntime: FATAL EXCEPTION: main
                                                              java.lang.NullPointerException
                                                                  at sk.spspb.android.More.<init>(More.java:34)
                                                                  at sk.spspb.android.MainActivity$1.onNavigationItemSelected(MainActivity.java:60)
                                                                  at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:168)
                                                                  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                  at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:89)
                                                                  at android.view.View.performClick(View.java:4439)
                                                                  at android.view.View$PerformClick.run(View.java:18395)
                                                                  at android.os.Handler.handleCallback(Handler.java:725)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                  at android.os.Looper.loop(Looper.java:176)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                  at dalvik.system.NativeStart.main(Native Method)
03-15 18:24:44.489 29236-29236/sk.spspb.android I/Process: Sending signal. PID: 29236 SIG: 9

请有人帮忙。

3 个答案:

答案 0 :(得分:1)

可以在任何方法之外声明ImageButtons,因为您在More()方法上使用它们,但初始化需要在onCreateView()内。

按照惯例,把它放在你的班级之上。

private ImageButton sib1;
private ImageButton imageButton3;

More()方法名称应该在lowerCase中作为约定。

然后:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ((MainActivity) getActivity()).setActionBarTitle("Kontakt");
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_more, container, false);
        sib1 = (ImageButton) v.findViewById(R.id.sib1);
        imageButton3 = (ImageButton) v.findViewById(R.id.imageButton3);
        return v;
    }

答案 1 :(得分:0)

这:

ImageButton sib1 = (ImageButton) getView().findViewById(R.id.sib1);
ImageButton imageButton3 = (ImageButton) getView().findViewById(R.id.imageButton3);

应位于onCreateView()

试试这个:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ((MainActivity) getActivity()).setActionBarTitle("Kontakt");
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_more, container, false);
        ImageButton sib1 = (ImageButton) v.findViewById(R.id.sib1);
        ImageButton imageButton3 = (ImageButton) v.findViewById(R.id.imageButton3);
        return v;
    }

此外,您需要这些才能使Click侦听器正常工作

sib1.setOnClickListener(this);
imageButton3.setOnClickListener(this);

答案 2 :(得分:0)

将sib1和imagebutton3初始化转移到onCreateView方法。

 ImageButton sib1,imageButton3; 

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ((MainActivity) getActivity()).setActionBarTitle("Kontakt");
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.fragment_more, container, false);
    sib1 = (ImageButton) v.findViewById(R.id.sib1);
    imageButton3 = (ImageButton) v.findViewById(R.id.imageButton3);
    More();
    return v;
}