2017-02-26 63 views
0

我按照这个教程对活动http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/如何使用与活动相同的片段?

创造一个良好的标签,但它使用的片段和片段没有不可能性使用任何正常的代码ES:

Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show(); 

或一个简单的按钮点击

btnSum =(Button)findViewById(R.id.button4); 
     btnSum.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mAuthTask = new TaskAsincrono(); 
       mAuthTask.execute((Void) null); 
      } 
     }); 

Fragment没有任何工作,它的可能性?

我需要的标签,因为活动有多重功能,结果是好的,但在级别的代码没有,1片段已登录与按钮,文本,但如果我叫按一下按钮不起作用例如:

Button btnSum =(Button)findViewById(R.id.button4); 
     btnSum.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

enter image description here

enter image description here

片段TAB2:

<RelativeLayout 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:id="@+id/tab2" 
    tools:context="net.appsite.Tab2"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Name" 
     android:ems="10" 
     android:layout_x="49dp" 
     android:layout_y="226dp" 
     android:id="@+id/editText8" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="59dp" /> 

    <Button 
     android:text="Button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button3" 
     android:layout_x="114dp" 
     android:layout_y="280dp" 
     android:elevation="0dp" 
     android:layout_marginTop="42dp" 
     android:layout_below="@+id/editText8" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

Tab2.jar:

public class Tab2 extends Fragment{ 

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


    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //ANY NORMAL CODE HERE NOT WORK, BUT IN NORMAL ACTIVITY WORK! 
     Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show(); 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_tab2, container, false); 
    } 
} 

这是我与Android首次应用,普莱舍帮助我。

+2

片段不延长'Context',因此你不能用'this'。如果您需要上下文,您可以使用'getContext()'或'getActivity()'。显示一个'吐司' –

回答

2

使用活动情境:

public class Tab2 extends Fragment{ 

    View view; 

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


    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //ANY NORMAL CODE HERE NOT WORK, BUT IN NORMAL ACTIVITY WORK! 
     Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show(); //use activity context 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     view =inflater.inflate(R.layout.fragment_tab2, container, false); 
     Button btnSum =(Button)view.findViewById(R.id.button3); // get Button with the view object 
     btnSum.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     return view; 
    } 
} 
+0

应用崩溃=( 显示java.lang.NullPointerException:尝试上的空对象引用调用虚拟方法“无效android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)” – user3477026

+0

你按钮ID是' button3'检查layout.xml – rafsanahmad007

+0

是的,非常感谢! – user3477026

1

你应该做getActivity().findViewById(..)Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show();Fragment

这是因为findViewByIdContextToast定义预计第一个参数是Context。由于Activity间接延伸Context,它的工作原理。但在Fragment您需要获取上下文。一种方法是在Fragment中执行getActivity()。用于显示Toast

点击监听器添加到Button使用碎片的view.findviewByID()

您的片段类应该看起来像

+0

谢谢!祝你有个美好的一天 – user3477026

+0

谢谢,和你一样!打开活动后 –

1

Fragment是不完全相同的是Activity请参阅docs了解更多信息


而且Fragment不是Context,看到更多的信息here所以你不能传递一个Context时使用this而是使用:

getActivity() 

getApplicationContext() 

而且绑定视图需要在you_fragment的onViewCreated方法中完成:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view =inflater.inflate(R.layout.fragment_tab2, container, false); 
    Button btnSum =(Button)view.findViewById(R.id.button4); 
    // note that button4 should be existing in fragment_tab2 
    btnSum.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       } 
      }); 

      // other views binding code 
      // and other code 
    return view; 
} 
2

在片段“this”不起作用,您必须获得像“getActivity()”这样的父活动的上下文或将活动传递到片段。

你的代码是这样

Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show(); 

TAB2活动如下:

public class Tab2 extends Fragment{ 
private View mRootView; 

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




public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    mRootView= inflater.inflate(R.layout.fragment_tab2, container,false); 

    Button btnSum =(Button)mRootView.findViewById(R.id.button4); 
    btnSum.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Your action goes here 
      } 
      }); 

     } 
    } 
相关问题