帮我解决这个错误。这是logcat和片段java代码。我想制作标签布局。在选项卡布局中,我想填充不同的java逻辑。但是,我不知道必须放置代码。请帮帮我
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: FATAL EXCEPTION: main
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: Process: com.prakosoft.physhics.calculator, PID: 27113
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at info.androidhive.materialtabs.fragments.OneFragment.onCreateView(OneFragment.java:39)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:731)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.View.measure(View.java:17637)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.os.Looper.loop(Looper.java:211)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5389)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
04-03 12:17:10.478 27113-27113/com.prakosoft.physhics.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
这是片段java代码
public OneFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
b = (EditText) getView().findViewById(R.id.b);
i = (EditText) getView().findViewById(R.id.i);
l = (EditText) getView().findViewById(R.id.l);
hasil = (TextView) getView().findViewById(R.id.hasil);
hitung = (Button) getView().findViewById(R.id.hitung);
spinner = (Spinner) getView().findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("");
list.add("0");
list.add("1/2");
list.add("1/2√2");
list.add("1/2√3");
list.add("1");
hitung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String be = b.getText().toString().trim();
String ii = i.getText().toString().trim();
String el = l.getText().toString().trim();
String text = spinner.getSelectedItem().toString();
if (spinner.getSelectedItem().toString().equals("1")) {
if (b.getText().toString().equals("") || i.getText().toString().equals("") || l.getText().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Isi terlebih dahulu", Toast.LENGTH_SHORT).show();
} else {
Integer bee = Integer.parseInt(be);
Integer iii = Integer.parseInt(ii);
Integer eel = Integer.parseInt(el);
Integer tekss = Integer.parseInt(text);
Integer gaya = (bee * iii * eel) * tekss;
hasil.setText("Gaya Lorentz = " + gaya);
}
} else if (spinner.getSelectedItem().toString().equals("1/2")) {
if (b.getText().toString().equals("") || i.getText().toString().equals("") || l.getText().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Isi terlebih dahulu", Toast.LENGTH_SHORT).show();
} else {
Integer bee = Integer.parseInt(be);
Integer iii = Integer.parseInt(ii);
Integer eel = Integer.parseInt(el);
Integer gaya = (bee * iii * eel) / 2;
hasil.setText("Gaya Lorentz = " + gaya);
}
} else if (spinner.getSelectedItem().toString().equals("1/2√2")) {
if (b.getText().toString().equals("") || i.getText().toString().equals("") || l.getText().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Isi terlebih dahulu", Toast.LENGTH_SHORT).show();
} else {
Integer bee = Integer.parseInt(be);
Integer iii = Integer.parseInt(ii);
Integer eel = Integer.parseInt(el);
double akar = Math.sqrt(2);
Integer gaya = (bee * iii * eel) / 2;
double hasilakar = gaya * akar;
hasil.setText("Gaya Lorentz = " + hasilakar);
}
} else if (spinner.getSelectedItem().toString().equals("1/2√3")) {
if (b.getText().toString().equals("") || i.getText().toString().equals("") || l.getText().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Isi terlebih dahulu", Toast.LENGTH_SHORT).show();
} else {
Integer bee = Integer.parseInt(be);
Integer iii = Integer.parseInt(ii);
Integer eel = Integer.parseInt(el);
double akar = Math.sqrt(3);
Integer gaya = (bee * iii * eel) / 2;
double hasilakar = gaya * akar;
hasil.setText("Gaya Lorentz = " + hasilakar);
}
} else if (spinner.getSelectedItem().toString().equals("0")) {
if (b.getText().toString().equals("") || i.getText().toString().equals("") || l.getText().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Isi terlebih dahulu", Toast.LENGTH_SHORT).show();
} else {
hasil.setText("Gaya Lorentz = 0");
}
} else if (spinner.getSelectedItem().toString().equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Pilih sin θ", Toast.LENGTH_SHORT).show();
}
}
});
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
return view;
}
答案 0 :(得分:1)
getView()
写成行
b = (EditText) getView().findViewById(R.id.b);
返回NULL,而不是使用此代码
b = (EditText) view.findViewById(R.id.b);