Cannot Resolve
This is one of my fragments that i am using in a navigation drawer. I need to add a button that when pressed, changes to a text box into different text. i keep getting errors that the findviewbyid()int cannot be resolved and such. I could get this working on MainActivity/activity_main, but when i use one of the pages of the nav drawer(like ConnectFragment.java/fragment_connect.xml), i get errors.
this is the code for just the ConnectFragment alone
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class ConnectFragment extends Fragment {
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
return rootView;
}
}
This is the code i want to implement
final Button ClassAButton = (Button)findViewById(R.id.ClassAButton);
ClassAButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View view){
TextView FeloniesText = (TextView)findViewById(R.id.FeloniesText);
FeloniesText.setText("Button 1 Has been pressed!");
}
}
);
Code from the XML (relative layout tag here, formatting got messed up)
http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/backgroundColor">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/ClassAButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/FeloniesText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="107dp" />
</RelativeLayout>
I also want to add multiple buttons to change the text view to different things, so i would be something like, Class A button prints out hi, Class B Button prints out yes, Class C Button prints out elephant, etc. Any help would be greatly appreciated. :D BTW API 15
Try this.
Change this line.
final Button ClassAButton = (Button)findViewById(R.id.ClassAButton);
To this.
final Button ClassAButton = (Button) rootView.findViewById(R.id.ClassAButton);
TextView FeloniesText = (TextView) rootView.findViewById(R.id.FeloniesText);
Edit : For text view try this.
ClassAButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
FeloniesText.setText("Button 1 Has been pressed!");
}
}
);
Replace this in your code.
Edit 2 :
Replace this full code with your code.
In reference link they have taken Activity and You have take fragment.
public class ConnectFragment extends Fragment {
final Button ClassAButton;
TextView FeloniesText;
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
init(rootView);
return rootView;
}
public void init(View view)
{
ClassAButton = (Button) view.findViewById(R.id.ClassAButton);
FeloniesText = (TextView) view.findViewById(R.id.FeloniesText);
ClassAButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
FeloniesText.setText("Button 1 Has been pressed!");
}
}
);
}
findViewById
is not part of Fragment
. It's part of ViewGroup
so instead of
(TextView)findViewById(R.id.FeloniesText);
you need to
(TextView)rootView.findViewById(R.id.FeloniesText);
链接地址: http://www.djcxy.com/p/84754.html
上一篇: 子级TextView的重力不适用于自定义ViewGroup
下一篇: 无法解析