2016-08-02 15 views
0

我需要通過單擊片段xml文件上的按鈕來打開一個新活動。如何通過單擊fragment xml文件上的按鈕來從片段java文件中打開新的Activity?

我已經編寫了一下我的片段Java文件通過點擊我的片段xml文件的按鈕調用新的活動

當我運行該應用程序,它崩潰

錯誤我得到的是在該行中的片段的java文件

「公共無效電子(查看視圖)」

08-02 10:58:55.654 3519-3519/com.example.mohammadzakriya.tabhost E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.mohammadzakriya.tabhost, PID: 3519 
    java.lang.IllegalStateException: Could not find method electronics(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2' 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

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:orientation="vertical" 
    tools:context=".FolderScale"> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="1" 
     android:textAlignment="center" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Electronics" 
     android:onClick="electronics"/> 

</LinearLayout> 

的Java

public class TopfreeFragment extends Fragment { 

    public TopfreeFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_topfree, container, false); 
    } 

    public void electronics(View view) { 
     Intent intent = new Intent(getActivity(),Electronicspage.class); 
     startActivity(intent); 
    } 
} 
+0

'ELECTRONICS'不等於'electronic' –

+1

你做了什麼錯誤? –

+0

把你的錯誤日誌放在這裏尋求更好的幫助 – Vickyexpert

回答

0

這裏是另一種方式,你可以用它來處理來自按鈕單擊事件:

public class TopfreeFragment extends Fragment implements View.OnClickListener{ 

    View view; 
    private Button btn; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.fragment_topfree, container, false); 
     btn = (Button) view.findViewById(R.id.button2); 
     btn.setOnClickListener(this); 
     return view; 
    } 

    public void onClick(View view) { 
     switch(view.getId()) { 
      case R.id.button2: 
       startActivity(new Intent(getActivity(),Electronicspage.class)); 
      break; 

      case R.id.XXXXX: 
      break; 
      // more button...... 

     } 
    } 
} 

這篇文章會爲你真正有用的:Android: how to handle button click

2

您正在使用electronicsonClick和方法是不electronicelectronics

使用electronics而不是electronic

1

根據我的代碼最後得到了一些eidt的答案的解決方案

謝謝@ vrund purohit鏈接How to handle button clicks using the XML onClick within Fragments

它幫助我瞭解最後的代碼如下

// Fragment java file updated 
 

 
public class TopfreeFragment extends Fragment implements View.OnClickListener { 
 

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

 
     View v = inflater.inflate(R.layout.fragment_topfree, container, false); 
 

 
     Button b = (Button) v.findViewById(R.id.button2); 
 
     b.setOnClickListener(this); 
 
     return v; 
 
    } 
 

 
    @Override 
 
    public void onClick(View v) { 
 
     switch (v.getId()) { 
 
      case R.id.button2: 
 
       Intent intent = new Intent(getActivity(),Electronicspage.class); 
 
       startActivity(intent); 
 
       break; 
 
     } 
 
    } 
 
}

相關問題