我的第二个片段(Test2
)只显示你好世界。
我的问题是每当我在第一个片段(((Test)getActivity()).display(a);
)中调用Test1
函数时,它会抛出NullPointerException
。
我无法检测为什么会抛出NullPointerException
,我已经尝试过实现它仍然没有工作的界面方法,给出了同样的错误。
我的主要活动
public class Test extends Activity implements Test1.OnFragmentInteractionListener,Test2.OnFragmentInteractionListener {
private GraphicalView show;
Intent intent;
int i;
static int a[]=new int[4];
String str;
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
FragmentTransaction transaction1=manager.beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
ActionBar bar = getActionBar();
bar.hide();
// bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3385FF")));
setContentView(R.layout.activity_bar_report);
intent=getIntent();
a=intent.getIntArrayExtra("Data");
display(a);
}
public void display(int a[])
{ Test1 test1 = new Test1();
if(TempValues.F_Id==0) {
test1.newInstance(a);
transaction.replace(android.R.id.content, test1);
}
else if(TempValues.F_Id==1)
{ Test2 test2 = new Test2();
transaction.replace(android.R.id.content, test2);
}
transaction.commit();
}
My 1st Fragment(Test1)
public class Test1 extends Fragment {
private OnFragmentInteractionListener mListener;
private GraphicalView show;
Intent intent;
int i;
static int a[]=new int[4];static String str;
static Bundle args;
Test test=new Test();
public Test1() {
}
public Test1 newInstance(int a[]) {
Test1 f = new Test1();
// Supply index input as an argument.
args = new Bundle();
args.putIntArray("index", a);
f.setArguments(args);
return f;
}
public interface OnFragmentInteractionListener {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_test1, container, false);
try{
a=args.getIntArray("index");
int c=0;
XYSeries series=new XYSeries("Report");
for(i=0;i<4;i++)
{
series.add(c+1,a[i]);
str+=String.valueOf(a[i]);
c++;
}
TextView text=(TextView)getActivity().findViewById(R.id.text);
text.setText(str);
XYMultipleSeriesDataset multipleSeriesDataset=new XYMultipleSeriesDataset();
multipleSeriesDataset.addSeries(series);
XYSeriesRenderer renderer=new XYSeriesRenderer();
renderer.setChartValuesTextSize(40);
renderer.setChartValuesTextAlign(Paint.Align.CENTER);
renderer.setLineWidth(4);
renderer.setDisplayChartValues(true);
renderer.setDisplayChartValuesDistance(1);
XYMultipleSeriesRenderer multipleSeriesRenderer=new XYMultipleSeriesRenderer();
multipleSeriesRenderer.addSeriesRenderer(renderer);
multipleSeriesRenderer.setBarSpacing(1);
multipleSeriesRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
multipleSeriesRenderer.setZoomEnabled(false, false);
multipleSeriesRenderer.setScale(1);
multipleSeriesRenderer.setXAxisMin(0);
multipleSeriesRenderer.setYAxisMin(0);
multipleSeriesRenderer.setXAxisMax(5);
multipleSeriesRenderer.setPanLimits(new double[]{0.0,4.0,0.0,Double.MAX_VALUE});
multipleSeriesRenderer.setLabelsTextSize(20);
multipleSeriesRenderer.setXLabelsColor(Color.BLACK);
multipleSeriesRenderer.setYLabelsColor(0,Color.BLACK);
multipleSeriesRenderer.setXLabels(0);
multipleSeriesRenderer.addXTextLabel(1, "Order \nBooking");
multipleSeriesRenderer.addXTextLabel(2, "Display/\nCounter Share");
multipleSeriesRenderer.addXTextLabel(3,"Hygiene \nAudit");
multipleSeriesRenderer.addXTextLabel(4,"VoC");
multipleSeriesRenderer.setXLabelsAlign(Paint.Align.CENTER);
show= ChartFactory.getBarChartView(getActivity(), multipleSeriesDataset, multipleSeriesRenderer, BarChart.Type.DEFAULT);
multipleSeriesRenderer.setClickEnabled(true);
multipleSeriesRenderer.setSelectableBuffer(10);
show.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ String str="";
SeriesSelection select=show.getCurrentSeriesAndPoint();
if(select.getXValue()==1) {
TempValues.F_Id = 1;
str = String.valueOf(TempValues.F_Id);
((Test)getActivity()).display(a);
int i = (int) select.getValue();
Toast.makeText(getActivity(), str + String.valueOf(i), Toast.LENGTH_LONG).show();
}
}
});
LinearLayout layout=(LinearLayout)getActivity().findViewById(R.id.BarChart);
layout.addView(show);
}catch (Exception e){TextView text=(TextView)getActivity().findViewById(R.id.text);
text.setText(e.toString());
}
return inflater.inflate(R.layout.fragment_test1, container, false);
}
这是我的logCat
02-26 14:19:56.583 30591-30591/com.example.administrator.edge E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.administrator.edge, PID: 30591
java.lang.NullPointerException
at com.example.administrator.edge.Test1$1.onClick(Test1.java:123)
at android.view.View.performClick(View.java:4470)
at android.view.View$PerformClick.run(View.java:18599)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您传递给事务的Test1
实例不是您设置参数的实例。 newInstance()
方法返回带有参数的实例,但它被丢弃。这意味着在事务实例中,args
中的onCreateView()
为空,并且当您调用NullPointerException
方法时,它会抛出getIntArray()
。按如下方式更改display()
方法:
public void display(int a[])
{
if(TempValues.F_Id==0) {
Test1 test1 = Test1.newInstance(a);
transaction.replace(android.R.id.content, test1);
}
...
}
此外,newInstance()
方法应该是静态的:
public static Test1 newInstance(int a[])
答案 1 :(得分:0)
1。 Diplay方法错误。
test1.newInstance(a);
执行此操作将返回Test1
的新实例,该实例使用您作为参数传递的数组,但该实例与test1
变量引用的实例不同。将newInstance
方法设为静态,并将其命名为:
Test1.newInstance(a);
2。请勿在{{1}}内拨打getActvity()
。原因是因为框架不能保证在onCreateView
调用Activity
时完全创建onCreateView
。您可以使用Fragment
回调。
出于同样的原因,这并不能保证每次都有效:
onActivityCreated
由于您不知道TextView text=(TextView)getActivity().findViewById(R.id.text);
text.setText(str);
是否确实已完全创建,因此在Activity
上调用findViewById
可能会返回Activity
,从而在此之后立即生成NullPointerException,当你拨打null
时。