我对此很陌生,并且设法获得了我的应用程序的布局,并且正在使用选项卡和片段。
每个布局都是一个不同的计算器,需要不同的计算才能运行。我正在努力弄清楚如何做到这一点。任何帮助深表感谢。
我要开始工作的第一件事是 BMI 片段的微调器设置。我是在 MainActivity 还是 Fragment 中执行此操作?从我到目前为止所做的事情来看,我得到了一个空指针异常。
这是我的 MainActivity 代码:
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position) {
case 0:
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
case 1:
Fragment fragment1 = new DummySectionFragment1();
Bundle args1 = new Bundle();
args1.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, position + 2);
fragment1.setArguments(args1);
return fragment1;
case 2:
Fragment fragment2 = new DummySectionFragment2();
Bundle args2 = new Bundle();
args2.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 3);
fragment2.setArguments(args2);
return fragment2;
case 3:
Fragment fragment3 = new DummySectionFragment3();
Bundle args3 = new Bundle();
args3.putInt(DummySectionFragment3.ARG_SECTION_NUMBER, position + 4);
fragment3.setArguments(args3);
return fragment3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 4 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
}
我的片段代码:
package com.HealthKicks.bodystats.admob;
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.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class DummySectionFragment extends Fragment {
EditText height_edit_text1, weight_edit_text1, height, weight;
Spinner height_spinner, weight_spinner;
Button calculatebmi;
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bmi, container, false);
setupSpinners();
height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);
weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
return rootView;
}
void setupSpinners() {
height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);
// TODO Auto-generated method stub
height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
height_edit_text1.setVisibility(View.GONE);
} else {
height_edit_text1.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
weight_edit_text1.setVisibility(View.GONE);
}
else if (position == 1){
weight_edit_text1.setVisibility(View.GONE);
}
else {
weight_edit_text1.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
日志猫:
10-10 18:38:40.959: E/AndroidRuntime(1118): FATAL EXCEPTION: main
10-10 18:38:40.959: E/AndroidRuntime(1118): java.lang.NullPointerException
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.HealthKicks.bodystats.admob.DummySectionFragment.setupSpinners(DummySectionFragment.java:33)
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.HealthKicks.bodystats.admob.DummySectionFragment.onCreateView(DummySectionFragment.java:26)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.os.Handler.handleCallback(Handler.java:730)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.os.Looper.loop(Looper.java:137)
10-10 18:38:40.959: E/AndroidRuntime(1118): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 18:38:40.959: E/AndroidRuntime(1118): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 18:38:40.959: E/AndroidRuntime(1118): at java.lang.reflect.Method.invoke(Method.java:525)
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 18:38:40.959: E/AndroidRuntime(1118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 18:38:40.959: E/AndroidRuntime(1118): at dalvik.system.NativeStart.main(Native Method)