2017-03-15 4 views
-1

私はAndroidのコーディングで初心者です。私はAndroidスタジオでBottomNavigationBarアクティビティテンプレートを使用して簡単なアプリを作成しました。フラグメント内で複数のImageButtonアクションを処理する

問題は、1つのフラグメントに2つのImageButtonを持ち、特定の電話番号で電話ダイヤラを開くことです。私は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 

誰かが助けてください。

答えて

1

を動作するようにクリックしてリスナーのためにこれらを必要とするそれは大丈夫ですMore()メソッドでそれらを使用しているので、どのメソッドの外にもImageButtonを宣言することができますが、初期化はonCreateView()の内部にある必要があります。

慣習として、これをクラスの上に置きます。

private ImageButton sib1; 
private ImageButton imageButton3; 

More()メソッド名は、慣習としてlowerCaseにする必要があります。

そして: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); 
     sib1 = (ImageButton) v.findViewById(R.id.sib1); 
     imageButton3 = (ImageButton) v.findViewById(R.id.imageButton3); 
     return v; 
    } 
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; 
    } 

はまた、あなたは

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

'onCreateView()'でビューを設定した後、あなたはまた 'getViewメソッド()findViewById(R.id.sib1)を行うことができます;' 'onResume()'の内部。 –

0

転送SIB1とimagebutton3を使う初期。

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; 
} 
関連する問題