我刚刚开始关注Android应用程序开发,并且对MainActivity的onCreate()中的布局进行夸大有疑问。可能我不是在问正确的问题。但基本上我有2个标签。我在第一个选项卡上有一个计算按钮,一旦点击就应该填充第二个选项卡上的表
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTableFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Details"));
tabLayout.addTab(tabLayout.newTab().setText("Discount"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
this.init();
}
public void onButtonClick(View view) {
EditText text = (EditText) findViewById(R.id.amount);
_amount = Integer.parseInt(text.getText().toString());
text = (EditText) findViewById(R.id.rate);
_rate = Integer.parseInt(text.getText().toString()) * 12;
EditText discount = (EditText) findViewById(R.id.discount);
discount.setText(Double.toString(_discount));
this.init();
}
public void init(){
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main);
if(tableLayout == null)
System.out.println("Table Layout is NULL");
else
System.out.println("Layout is not null");
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("Date");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Amount ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Discount ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
for (int i = 0; i < _loanTerm; ++i) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
tableLayout.addView(tbrow);
}
}
}
public class MyTableFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_table, container, false);
}
}
public class PagerAdapter extends FragmentStatePagerAdapter {
private int _numOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this._numOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return _numOfTabs;
}
以下是activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
以下是tab_fragment_1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AMOUNT"
android:id="@+id/textView"
android:layout_alignBottom="@+id/Amount"
android:layout_toStartOf="@+id/calculateButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/Amount"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RATE"
android:id="@+id/textView2"
android:layout_marginTop="32dp"
android:layout_below="@+id/Amount"
android:layout_alignEnd="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/rate"
android:layout_alignBottom="@+id/textView2"
android:layout_alignStart="@+id/Amount" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DISCOUNT"
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/calculateButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/discount"
android:editable="false"
android:inputType="none"
android:layout_alignBottom="@+id/textView4"
android:layout_alignStart="@+id/rate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/CALCULATE"
android:id="@+id/calculateButton"
android:clickable="true"
android:enabled="true"
android:onClick="onButtonClick"
android:layout_above="@+id/payment"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp" />
</RelativeLayout>
以下是tab_fragment_2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
这是我的my_table.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="@+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
当我尝试访问时,在我的init()中
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main);
它返回null
我错过了什么?
由于
答案 0 :(得分:1)
如果你想膨胀一个片段,首先应该在activity main中有一个framelayout,然后替换所选片段的framelayout。
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//change content_frame for the id fiven to the frame layout inside activity_main
getFragmentManager().beginTransaction().add(R.id.content_frame, new MyTableFragment()).commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Loan Details"));
tabLayout.addTab(tabLayout.newTab().setText("Amortization"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
this.init();
}
}
public class MyTableFragment extends Fragment { @覆盖 public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ return inflater.inflate(R.layout.my_table,container,false); } }
public class MyTableFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.my_table, container, false);
//in here you find the table using the view
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tab_layout);
return view;
}
}
答案 1 :(得分:0)
您必须首先在活动中使用您的片段
请执行以下操作:
首先在主活动布局中定义容器以托管您的片段并为其分配id,假设此id为“fragment_container”, 在行之后:
setContentView(R.layout.activity_main);
添加以下行:
getFragmentManager().beginTransaction().add(R.id.fragment_container, new MyTableFragment()).commit();