作者gndelo (狄乐)
看板AndroidDev
标题[问题] 呼叫fragment前的错误
时间Sun Sep 13 21:10:46 2015
各位前辈大家好
小弟练习开发app中
UI架构为MainActivity→GameActivity→GameFragment(在这做资料的统计)
(设定一个Button按下後出现)
↓
→StatictiscFragment(统计的结果显示在这)
问题出在还没按下显示的Button前任何需要调用到StatictiscFragment元件变数的动作都
会造成程序崩溃,我实在找不出问题出在哪里...
public TextView mcounttime;
mcounttime = (TextView) getActivity().findViewById(R.id.txt_time);
private View.OnClickListener btnSTARTOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
yournum = "";//将答题缓存值设为空值
check = true;//将启动代码设为true
fincode = ran.nextInt(100) + 1;//随机初始化正确值
min = "1";//将最小缓存值设为1
max = "100";//将最大缓存值设为100
mtxtmin.setText(R.string.min_1);
mtxtmax.setText(R.string.max_100);
mtxtyour.setText(R.string.empty);
mtxtres.setText(R.string.btn_start);
icounttime++;
mcounttime.setText(String.valueOf(icounttime));
}
};
以上红字部分的mcounttime就是StatictiscFragment的元件变数
---------------补充StatictiscFragment的部分---------------
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class StatisticsFragment extends Fragment {
public StatisticsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_statistics, container,
false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
GameFragment gamefrag = (GameFragment) getFragmentManager().findFragmentById(R.id.game);
gamefrag.mcounttime = (TextView) getActivity().findViewById(R.id.txt_time);
gamefrag.mcountguess = (TextView) getActivity().findViewById(R.id.txt_guesstime);
//gamefrag.mcountbingo = (TextView) getActivity().findViewById(R.id.txt_bingotime);
//gamefrag.mcountpercent = (TextView) getActivity().findViewById(R.id.txt_percent);
}
}
--
听别人说施比受更有福
我倒认为吃比瘦更有福
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.231.61.44
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1442149856.A.F6B.html
※ 编辑: gndelo (61.231.61.44), 09/13/2015 21:12:26
king大 我并不是抓不到StatictiscFragment的元件
而是在GameFragment设一个Button使用add方法将StatictiscFragment加入主画面
但在按下那个Button前使用到StatictiscFragment的元件变数似乎会回传null
但如果一开始先按下Button呼叫过的话就一切正常
2F:→ loxyz: 错误讯息?StatictiscFragment一开始有先建立吗? 09/13 23:12
如上补充
3F:→ corrupt003: 一般的做法应该是把资料带入fragment做初始化吧 09/14 07:46
可是我的目的是要在GameFragment里的动作能够先统计再交由StatictiscFragment显示
※ 编辑: gndelo (1.171.240.247), 09/15/2015 19:23:20
5F:→ corrupt003: 动态产生可以带资料的 Fragment 09/15 21:09
6F:→ corrupt003: 会有 null 大概是还没用 FragmentTransaction.add() 09/15 21:15
7F:→ corrupt003: StatictiscFragment 不会跑到 onCreateView() 09/15 21:16
感谢 虽然还没完全看懂newInstance的用法
突然发现把
setText的部分放进StatictiscFragment就解决了...
※ 编辑: gndelo (39.9.226.166), 09/18/2015 20:32:49