Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 空指针验证,android.widget.TableLayout.getChildAt(int)_Java_Android_Null_Destroy - Fatal编程技术网

Java 空指针验证,android.widget.TableLayout.getChildAt(int)

Java 空指针验证,android.widget.TableLayout.getChildAt(int),java,android,null,destroy,Java,Android,Null,Destroy,我是android开发的新手,当我点击我的应用程序时,它停止工作,因为它不能破坏表格布局 ....................................................................................................................................................................................................... 这是我在调

我是android开发的新手,当我点击我的应用程序时,它停止工作,因为它不能破坏表格布局 ....................................................................................................................................................................................................... 这是我在调试器控制台中得到的

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.mohannad.athkarapp, PID: 7139
              java.lang.RuntimeException: Unable to destroy activity      {com.mohannad.athkarapp/com.mohannad.athkar`enter code here`app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TableLayout.getChildAt(int)' on a null object reference
                  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4203)
这是我的代码:

public class tab2fragment extends Fragment {
public static TableLayout tableLayout;
public static TableRow tableRow;
public static TextView id0Count;
public static TextView id1Count;
public static TextView id2Count;
public static TextView id3Count;
public static TextView id4Count;

public tab2fragment() {
    // Required empty public constructor
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

   refreshF();

}

@Override
public void onStart() {
    super.onStart();
    refreshF();
}

public void refreshF(){

    tableLayout = (TableLayout) getActivity().findViewById(R.id.ourTable);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    id0Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(2);
    id1Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(3);
    id2Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(4);
    id3Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(5);
    id4Count = (TextView) tableRow.getChildAt(0);
    MainActivity.CountZ1= getActivity().getSharedPreferences(MainActivity.zekr1,0);
    MainActivity.CountZ2 = getActivity().getSharedPreferences(MainActivity.zekr2,0);
    MainActivity.CountZ3 = getActivity().getSharedPreferences(MainActivity.zekr3,0);
    MainActivity.CountZ4 = getActivity().getSharedPreferences(MainActivity.zekr4,0);
    MainActivity.CountZ5 = getActivity().getSharedPreferences(MainActivity.zekr5,0);
    id0Count.setText(""+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
    id1Count.setText(""+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
    id2Count.setText(""+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
    id3Count.setText(""+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
    id3Count.setText(""+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));
}
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.tab2fragment, container, false);
}
@Override
public void onResume() {

    super.onResume();
    refreshF();
}



@Override
public void onDetach() {
    super.onDetach();
    refreshF();
}
}

public类MainActivity扩展了AppCompatActivity{
公共静态最终字符串switchp=“Switchpref”;
公共静态最终字符串radio=“radioPref”;
公共静态最终字符串zekr1=“MyPrefsFileZ1”;
公共静态最终字符串zekr2=“MyPrefsFileZ2”;
公共静态最终字符串zekr3=“MyPrefsFileZ3”;
公共静态最终字符串zekr4=“MyPrefsFileZ4”;
公共静态最终字符串zekr5=“MyPrefsFileZ5”;
公共静态SharedReferences CountZ1;
公共静态共享引用CountZ2;
公共静态共享引用CountZ3;
公共静态共享引用CountZ4;
公共静态共享引用CountZ5;
公共静态共享引用切换状态;
公共静态共享引用状态;
@凌驾
public void onBackPressed(){
super.onBackPressed();
完成();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager ViewPager=(ViewPager)findViewById(R.id.pager);
ViewPagerAdapter适配器=新的ViewPagerAdapter(getSupportFragmentManager());
//将片段逐个添加到适配器
adapter.addFragment(新的tab1fragment();
addFragment(新的tab2fragment(),“السسل”);
adapter.addFragment(新的tab3fragment(),“adapter.addFragment”);
viewPager.setAdapter(适配器);
TabLayout TabLayout=(TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
公共空间(视图){
TextView v=findViewById(R.id.textcount);
tab1fragment.spinner=(spinner)findviewbyd(R.id.spinner);
振动(200);
SharedReferences.Editor editor1=MainActivity.CountZ1.edit();
SharedReferences.Editor editor2=MainActivity.CountZ2.edit();
SharedReferences.Editor editor3=MainActivity.CountZ3.edit();
SharedReferences.Editor editor4=MainActivity.CountZ4.edit();
SharedReferences.Editor editor5=MainActivity.CountZ5.edit();
int pos=tab1fragment.spinner.getSelectedItemPosition();
开关(pos){
案例0:tab1fragment.count1=0;
tab2fragment.id0Count.setText(“+0”);
editor1=MainActivity.CountZ1.edit();
editor1.putInt(MainActivity.zekr1,0);
editor1.commit();
v=findViewById(R.id.textcount);
v、 setText(“+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
打破
案例1:tab1fragment.count2=0;
tab2fragment.id1Count.setText(“+0”);
editor2=MainActivity.CountZ2.edit();
编辑器2.putInt(MainActivity.zekr2,0);
editor2.commit();
v=findViewById(R.id.textcount);
v、 setText(“+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
打破
案例2:tab1fragment.count3=0;
tab2fragment.id2Count.setText(“+0”);
editor3=MainActivity.CountZ3.edit();
编辑器3.putInt(MainActivity.zekr3,0);
editor3.commit();
v=findViewById(R.id.textcount);
v、 setText(“+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
打破
案例3:tab1fragment.count4=0;
tab2fragment.id3Count.setText(“+0”);
editor4=MainActivity.CountZ4.edit();
编辑器4.putInt(MainActivity.zekr4,0);
editor4.commit();
v=findViewById(R.id.textcount);
v、 setText(“+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
打破
案例4:tab1fragment.count5=0;
tab2fragment.id4Count.setText(“+0”);
editor5=MainActivity.CountZ5.edit();
编辑器5.putInt(MainActivity.zekr5,0);
editor5.commit();
v=findViewById(R.id.textcount);
v、 setText(“+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));
打破
}
}
//使用FragmentPagerAdapter的viewpager适配器
类ViewPagerAdapter扩展了FragmentPagerAdapter{
私有最终列表MFFragmentList=new ArrayList();
私有最终列表MFFragmentTitleList=new ArrayList();
公共视图页面编辑器(碎片管理器){
高级经理;
}
@凌驾
公共片段getItem(int位置){
返回MFFragmentList.get(位置);
}
@凌驾
public int getCount(){
返回MFFragmentList.size();
}
public void addFragment(片段片段,字符串标题){
添加(片段);
MFFragmentTitleList.add(标题);
}
@凌驾
公共字符序列getPageTitle(int位置){
返回MFFragmentTitleList.get(位置);
}
}
void doVibrate(整数num){
可控震源x=(可控震源)getSystemService(this.可控震源_服务);
x、 振动(num);
}

}

我提供了一种快速的解决方案,但我也建议您学习Android基础知识,了解片段和活动的生命周期

tableLayout = (TableLayout) getActivity().findViewById(R.id.ourTable);
if(tableLayout != null ){
tableRow = (TableRow) tableLayout.getChildAt(1);
id0Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(2);
id1Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(3);
id2Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(4);
id3Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(5);
id4Count = (TextView) tableRow.getChildAt(0);
MainActivity.CountZ1= getActivity().getSharedPreferences(MainActivity.zekr1,0);
MainActivity.CountZ2 = getActivity().getSharedPreferences(MainActivity.zekr2,0);
MainActivity.CountZ3 = getActivity().getSharedPreferences(MainActivity.zekr3,0);
MainActivity.CountZ4 = getActivity().getSharedPreferences(MainActivity.zekr4,0);
MainActivity.CountZ5 = getActivity().getSharedPreferences(MainActivity.zekr5,0);
id0Count.setText(""+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
id1Count.setText(""+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
id2Count.setText(""+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
id3Count.setText(""+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
id3Count.setText(""+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));

}
下面这段代码毫无意义

 @Override
public void onDetach() {
    super.onDetach();
    refreshF();
}

非常感谢它奏效了,还有什么建议吗?这是我第一次使用碎片,所以是的,我想我需要更多地理解基础知识
 @Override
public void onDetach() {
    super.onDetach();
    refreshF();
}