我有两个活动,父母和孩子。使用Intent发生2之间的数据传递。我根据微调器的值设置一个或两个选项卡。标签和TableLayout显示在第二页上。
问题是:当我设置FragmentStatePageAdapter
时,onCreateView
中的TabFragment1
没有被调用,程序因错误而崩溃
Abhishek
错误:
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TableLayout.addView(android.view.View,android.view.ViewGroup $ LayoutParams)”
在finance.banking.comparefd.CalFDActivity.addHeaders(CalFDActivity.java:292)
在finance.banking.comparefd.CalFDActivity.onCreate(CalFDActivity.java:205)
在android.app.Activity.performCreate(Activity.java:6237)
代码:
package finance.banking.comparefd;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
//import android.support.v4.view.PagerAdapter;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Testing"));
final EditText amtDeposited = findViewById(R.id.amtDeposit);
Button button=findViewById(R.id.btnCalc);
// Spinner element
final Spinner spinner = findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("--Select--");
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("3-5");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.
setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item
);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(), result.toString(),
Toast.LENGTH_LONG).show();
final RadioGroup radioGroup = findViewById(R.id.radio_group);
Toast.makeText(getBaseContext(),
String.valueOf(radioGroup.getCheckedRadioButtonId()),
Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), ((RadioButton)
findViewById(radioGroup.getCheckedRadioButtonId()))
.getText().toString(),
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(),
CalFDActivity.class);
startActivity(intent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
// On selecting a spinner item
TableLayout tl = findViewById(R.id.table1);
TabLayout tabLayout3 = findViewById(R.id.tab_layout);
TableLayout tlH = findViewById(R.id.headerTable);
if (tl!=null ) {
tabLayout3.removeAllTabs();
tabLayout3.addTab(tabLayout3.newTab().setText("Testing"));
tl.removeAllViews();
tlH.removeAllViews();
}
// Showing selected spinner item
// Toast.makeText(parent.getContext(), "Selected: " + item,
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
package finance.banking.comparefd;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class CalFDActivity extends AppCompatActivity {
List<> tabData = new ArrayList<>();
ViewPager v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calfd);
Intent intent = getIntent();
TabLayout tabLayout = findViewById(R.id.tab_layout1);
v = selectTabFragment(tabLayout);
showData(v, tabLayout);
Log.d("Android Fragment ", "" + v);
TableLayout tlH = v.findViewById(R.id.headerTable);
TableLayout tl = v.findViewById(R.id.table1);
//tl.removeAllViews();
addHeaders(tlH);
addData(tl, tabData);
}
private void showData(final ViewPager v, TabLayout tabLayout) {
v.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 1) {
tabData = new ArrayList<>();
TableLayout tlH = findViewById(R.id.headerTable1);
TableLayout tl = findViewById(R.id.table2);
tlH.removeAllViews();
tl.removeAllViews();
addHeaders(tlH);
addData(tl, tabData);
}
v.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void addData(TableLayout tl, List<FDData> tabData) {
for (int i = 0; i < tabData.size(); i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(getLayoutParams());
tr.addView(getTextView(i + 1, tabData.get(i).getName(),
Color.BLACK, Typeface.NORMAL, Color.WHITE, 20, 30));
tr.addView(getTextView(i + 2, tabData.get(i).getCity(),
Color.BLACK, Typeface.NORMAL, Color.WHITE, 30, 30));
tr.addView(getTextView(i + 3, tabData.get(i).getAddress,
Color.BLACK, Typeface.NORMAL, Color.WHITE, 20, 30));
tr.addView(getTextView(i + 4, tabData.get(i).getHobby,
Color.BLACK, Typeface.NORMAL, Color.WHITE, 20, 30));
tl.addView(tr, getLayoutParams());
}
}
public void addHeaders(TableLayout tl) {
TableRow tr = new TableRow(this);
Log.d("Android: ", "Before 1");
tr.setLayoutParams(getLayoutParams());
Log.d("Android: ", "After 1");
tr.addView(getTextView(1, "Name", Color.WHITE, Typeface.BOLD,
Color.BLUE, 20, 80));
Log.d("Android: ", "After 2");
tr.addView(getTextView(2, "City", Color.WHITE, Typeface.BOLD,
Color.BLUE, 30, 80));
tr.addView(getTextView(3, "Address", Color.WHITE, Typeface.BOLD,
Color.BLUE, 20, 80));
tr.addView(getTextView(4, "Hobby", Color.WHITE, Typeface.BOLD,
Color.BLUE, 20, 80));
Log.d("Android: ", "After 3");
tl.addView(tr, getTblLayoutParams());
Log.d("Android: ", "After 4");
}
private TableRow.LayoutParams getLayoutParams() {
/* LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);*/
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(2, 2, 2, 2);
return params;
}
private TextView getTextView(int id, String title, int color, int
typeface, int bgColor, int widthInPercentOfScreenWidth, int
fixedHeightInPixels) {
TextView tv = new TextView(this);
tv.setId(id);
tv.setText(title);
tv.setTextColor(color);
tv.setGravity(Gravity.CENTER);
//tv.setPadding(0, 10, 10, 0);
tv.setTypeface(Typeface.DEFAULT, typeface);
tv.setBackgroundColor(bgColor);
tv.setLayoutParams(getLayoutParams());
tv.setWidth(100);
tv.setHeight(fixedHeightInPixels);
//tv.setOnClickListener(this);
return tv;
}
private TableLayout.LayoutParams getTblLayoutParams() {
Log.d("Android: ", "Inside getTblLayoutParams");
return new TableLayout.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
}
public ViewPager selectTabFragment(TabLayout tabLayout) {
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
v = findViewById(R.id.pager);
CustomPagerAdapter adapter = new
CustomPagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount(),getBaseContext());
v.setAdapter(adapter);
return v;
}
}
package finance.banking.comparefd;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
Log.d("Android: ", "Inside TabFragment1");
return inflater.inflate(R.layout.tab_fragment_1, container, false);
}
}
package finance.banking.comparefd;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
public class CustomPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
Context context;
public CustomPagerAdapter(FragmentManager fm, int NumOfTabs, Context
context) {
super(fm);
Log.d("Android: ", "Inside Constructor");
this.mNumOfTabs = NumOfTabs;
this.context = context;
}
@Override
public Fragment getItem(int position) {
Log.d("Android: ","Inside PagerAdapter");
Log.d("Position: ",String.valueOf(position));
switch (position) {
case 0:
//TabFragment1 tab1 = (TabFragment1)
Fragment.instantiate(context, TabFragment1.class.getName());
return new TabFragment1();
case 1:
//TabFragment2 tab2 = (TabFragment2)
Fragment.instantiate(context, TabFragment2.class.getName());
return new TabFragment2();
case 2:
//TabFragment3 tab3 = (TabFragment3)
Fragment.instantiate(context, TabFragment3.class.getName());
return new TabFragment3();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}