[英]findviewbyid - Cannot Resolve
This is one of my fragments that I am using in a navigation drawer
. 这是我在
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. 我不断收到
findviewbyid()
int之类的错误。 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. 我可以在
MainActivity/activity_main
,但是当我使用导航抽屉的页面之一(如ConnectFragment.java/fragment_connect.xml
)时,出现错误。
This is the code for just the ConnectFragment alone 这是仅ConnectFragment的代码
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) 来自XML的代码(此处为相对布局标记,格式混乱)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/xmlns:tools="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. 我还想添加多个按钮以将文本视图更改为不同的内容,所以我会像是,A类按钮打印出嗨,B类按钮打印出是,C类按钮打印出大象,等等。不胜感激。 :D BTW API 15
: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 : 编辑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
. findViewById
不是Fragment
一部分。 It's part of ViewGroup
so instead of 它是
ViewGroup
的一部分,所以不是
(TextView)findViewById(R.id.FeloniesText);
you need to 你需要
(TextView)rootView.findViewById(R.id.FeloniesText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.