微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java-片段中间(II):在片段中动态添加行,Android

我试图动态地向片段中的表添加行.
但是,我遇到了一些运行时错误,非常需要一些建议.

logcat的:

1)E / AndroidRuntime(1038):java.lang.RuntimeException:无法启动活动ComponentInfo {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):at 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);

TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);

这是一个避免NullPointerException的错误.

并使用getActivity()初始化视图.要知道为什么要读

When to call activity context OR application context?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐