0

我正在使用“Fixed Tabs + Swipe”模型的 android 应用程序中开发一个活动。此活动中的每个“选项卡”都将用于存储不同类型的联系信息。第一个存储姓名、性别、手机、出生日期等。下一个选项卡将存储地址信息 - 后面的选项卡将存储其他信息。

所以在第一个选项卡上,我想要一个类似于“下拉菜单”的微调器,用于选择人的头衔(先生/女士/夫人等)。我的strings.xml定义中有一个字符串数组,如下所示:

<string-array name="titles">
    <item>"Mr."</item>
    <item>"Miss"</item>
    <item>"Ms."</item>
    <item>"Mrs."</item>
    <item>"Dr."</item>        
</string-array>

但是,我正在尝试在代码中填充微调器,并且在线上出现空指针异常titleSpinner.setAdapter(adapter);。这是我的活动代码:

public class ModifyCustActivity extends FragmentActivity implements
        ActionBar.TabListener {

private static final int NUM_PAGES = 2;
private static final int DATE_DIALOG = 0;

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modify_cust);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayHomeAsUpEnabled(true);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
    setupTitleSpinner();
}

private void setupTitleSpinner() {      
    Spinner titleSpinner = (Spinner) findViewById(R.id.spnTitle);       
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.titles, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    titleSpinner.setAdapter(adapter); // EXCEPTION OCCURS HERE
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.modify_cust, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new DummySectionFragment();
        switch (position) {
        case 0:
            return fragment = new FragmentCustBasic();
        case 1:
            return fragment = new FragmentCustAddress();
        default:
            break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return "Basic";
        case 1:
            return "Address";
        }
        return null;
    }
}

public static class FragmentCustBasic extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    public FragmentCustBasic() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_modify_cust_basic, container, false);
                return rootView;
    }
}

public static class FragmentCustAddress extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    public FragmentCustAddress() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_modify_cust_address, container, false);
                return rootView;
    }
}

public static class DummySectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(
                R.layout.fragment_modify_cust_basic, container, false);
        return rootView;
    }
}
}

这是活动的 xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/androidbg_dark"
    tools:context=".ModifyCustActivity" />

这是其中一个片段的 xml 文件(有问题的片段 - 上面有微调器):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ModifyCustActivity$DummySectionFragment" >

    <EditText
        android:id="@+id/txtFirstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="@string/fname"
        android:inputType="textPersonName|textCapWords"
        android:textColor="#FFFFFF" >

        <requestFocus />
       </EditText>

    <EditText
        android:id="@+id/txtLastName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtFirstName"
        android:ems="10"
        android:hint="@string/lname"
        android:inputType="textPersonName|textCapWords"
        android:textColor="#FFFFFF" >

    </EditText>

    <EditText
        android:id="@+id/txtMobileNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtLastName"
        android:ems="10"
        android:hint="@string/mobile"
        android:inputType="phone"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/txtDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtMobileNumber"
        android:ems="10"
        android:hint="@string/dob"
        android:inputType="date"
        android:textColor="#FFFFFF" />

    <Spinner
        android:id="@+id/spnTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtDate"
        android:textColor="#FFFFFF" />

</RelativeLayout>

这是我的 logcat 输出:

08-01 11:33:02.445: E/AndroidRuntime(10461): FATAL EXCEPTION: main
08-01 11:33:02.445: E/AndroidRuntime(10461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.ModifyCustActivity}: java.lang.NullPointerException
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.os.Looper.loop(Looper.java:137)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at dalvik.system.NativeStart.main(Native Method)
08-01 11:33:02.445: E/AndroidRuntime(10461): Caused by: java.lang.NullPointerException
08-01 11:33:02.445: E/AndroidRuntime(10461):    at com.example.myapp.ModifyCustActivity.setupTitleSpinner(ModifyCustActivity.java:102)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at com.example.myapp.ModifyCustActivity.onCreate(ModifyCustActivity.java:93)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.Activity.performCreate(Activity.java:5206)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-01 11:33:02.445: E/AndroidRuntime(10461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-01 11:33:02.445: E/AndroidRuntime(10461):    ... 11 more

我是android开发的新手。

4

3 回答 3

0

您应该将微调器全局清除:

所以

public class ModifyCustActivity extends FragmentActivity implements ActionBar.TabListener {

  Spinner spinner;
  @Override
  protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_modify_cust);
     spinner = (Spinner) findViewById(R.id.spnTitle);
      .....
     setupTitleSpinner();

  }
  private void setupTitleSpinner() {      

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.titles, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter); // spinner is now global
 }
}

因为您试图访问未定义的数据。因为您的 findViewById 可以找到项目,因为您已经取消了“setContentView”

于 2013-08-01T11:14:55.350 回答
0

activity_modify_cust.xml 中的 ViewPager 视图在哪里。那么您正在尝试获取不在 xml 文件中的 viewId。下面给出

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter); 

// 所以这一行给你空指针异常

于 2013-08-01T11:13:17.063 回答
0

我遇到这个问题是因为我试图访问的视图包含在一个单独的片段 xml 文件中。我已经问了一个关于如何在此处执行此操作的单独问题。谢谢你们的回复。

于 2013-08-01T12:40:02.880 回答