Java 片段中间体(II):在片段中动态添加行,Android
我试图在片段中向表动态添加行。 然而,我面临着一些运行时错误,非常需要一些建议 日志: 1)Java 片段中间体(II):在片段中动态添加行,Android,java,android,android-fragments,android-tablelayout,Java,Android,Android Fragments,Android Tablelayout,我试图在片段中向表动态添加行。 然而,我面临着一些运行时错误,非常需要一些建议 日志: 1) E/AndroidRuntime(1038):java.lang.RuntimeException:无法启动活动组件信息{com.example.lol/com.example.lol.MainActivity}:android.view.InflateException:二进制XML文件行#96:膨胀类片段时出错 2) E/AndroidRuntime(1038):在android.app.Activ
E/AndroidRuntime(1038):java.lang.RuntimeException:无法启动活动组件信息{com.example.lol/com.example.lol.MainActivity}:android.view.InflateException:二进制XML文件行#96:膨胀类片段时出错
2) E/AndroidRuntime(1038):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
3) E/AndroidRuntime(1038):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
FragmentTwo.Java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_two, container, false);
TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);
TableRow tr = new TableRow(getActivity().getApplicationContext());
// tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(getActivity().getApplicationContext());
textview1.setText("unhappy");
tr.addView(textview1);
TextView textview2 = new TextView(getActivity().getApplicationContext());
textview2.setText("happy");
tr.addView(textview2);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
return view;
}
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
fragment_two.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<TableRow
android:id="@+id/infoRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Unhappy"
android:id="@+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="Happy"
android:id="@+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</TableRow>
</TableLayout>
</LinearLayout>
更改此选项
TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);
到
这是一个可以避免NullPointerException
的错误
并使用getActivity()
初始化视图。想知道为什么要读书吗
我遇到了同样的问题,这是因为我传递了应用程序上下文,而不是活动上下文
TableRow tr = new TableRow(getActivity());
TextView textview1 = new TextView(getActivity());
textview1.setText("unhappy");
tr.addView(textview1);
TextView textview2 = new TextView(getActivity());
textview2.setText("happy");
tr.addView(textview2);
你也应该读这本书
张贴整个stacktraceThanks以获得指导。我理解为什么我们使用getActivity()而不是getApplicationContext(),后者比activity持续时间更长。您的建议有效,但现在我遇到了另一个问题。以前有2个按钮,单击每个按钮将直接将用户链接到相应的片段。当我实现这个解决方案时,片段1的按钮不起作用。我应该把这个问题放到另一个帖子上吗?请寻求您的帮助advice@lonelearner如果你做对了,它会起作用的。是的,新人发布一个新问题,编辑这个问题将完全改变。对于那些了解更多关于片段的人,请参考我的帖子亲爱的先生,我已经在下面的url中发布了我的新问题。谢谢你的建议
TableRow tr = new TableRow(getActivity());
TextView textview1 = new TextView(getActivity());
textview1.setText("unhappy");
tr.addView(textview1);
TextView textview2 = new TextView(getActivity());
textview2.setText("happy");
tr.addView(textview2);