我关注了这个人的 tutorial关于如何制作 ActionBar。假设我想更改其中一个 fragment 中的 TextView。所以我在 onCreate 下的 StartActivity.java 中添加了这个:
TextView textview = (TextView) findViewById(R.id.textView1);
textview.setText("HI!");
当我启动我的应用程序时,它崩溃了。有人能指出我正确的方向吗?
我希望有人花时间看看这个人的教程,因为他的布局与我的布局基本相同。谢谢。
最佳答案
如果你想改变你的组件,我建议你在 fragment 中创建一个这样的方法:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.detailsText);
textView.setText(text);
}
}
关于android - 更改 Fragment 中的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539029/