0

我是 android 新手,我试图让 editText 在选择项目时变得可见,代码似乎很好,但是只要我运行应用程序并单击访问该活动的按钮,模拟器就会停止工作。这是我的代码:

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ManageActivity extends FragmentActivity implements
        ActionBar.TabListener {



    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.manage, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

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

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

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a NewCaseFragment (defined as a static inner class
            // below) with the page number as its lone argument.

            if(position == 0){
            Fragment fragment = new NewCaseFragment();
            //Bundle args = new Bundle();
            //args.putInt(NewCaseFragment.ARG_SECTION_NUMBER, position + 1);
            //fragment.setArguments(args);
            return fragment;

            }
            else{
                Fragment fragment = new UpdateFragment();
                return fragment;
            }
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @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);
            }
            return null;
        }
    }

    /**
     * A  fragment representing a section of the app
     */
    public static class NewCaseFragment extends Fragment implements OnItemSelectedListener{


        public NewCaseFragment() {
        }

        Button members ;
        EditText orgname ,  orgaddress;
        //Spinner spinner1;




       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.newcase,
                    container, false);

            Button members = (Button)getActivity(). findViewById(R.id.members);
            EditText orgname = (EditText)getActivity(). findViewById(R.id.orgname);
            EditText orgaddress = (EditText)getActivity(). findViewById(R.id.orgaddress);

            Spinner spinner1 = (Spinner)getActivity(). findViewById(R.id.spinner1);

            spinner1.setOnItemSelectedListener(this);           
            //members.setOnClickListener(membersListener);

            return rootView;
        }

       public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
       {
        orgname.setVisibility(View.VISIBLE);
        orgaddress.setVisibility(View.VISIBLE);

       }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }


    }


    /*******
     * 
     */

public static class UpdateFragment extends Fragment {


        public UpdateFragment() {
        }

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.updatecase,
                    container, false);

            return rootView;
        }
    }


}

布局 :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.01"
            android:orientation="horizontal"
            android:padding="10dp" >

            <EditText
                android:id="@+id/casename"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:ems="10"
                android:hint="@string/casename"
                android:inputType="text"
                android:textSize="12sp" >

                <requestFocus />
            </EditText>
        </LinearLayout>

        <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/organisation"
        android:prompt="@string/infoorg" />




        <!-- layout for organisation information -->



        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.01"
            android:orientation="horizontal"
            android:padding="10dp" >

            <EditText
                android:id="@+id/orgname"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:hint="@string/orgname"
                android:inputType="text"
                android:textSize="12sp" 
                android:visibility="gone"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.01"
            android:orientation="horizontal"
            android:padding="10dp" >

            <EditText
                android:id="@+id/orgaddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:hint="@string/orgaddress"
                android:inputType="text"
                android:textSize="12sp"
                android:visibility="gone"
                 />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.01"
            android:orientation="horizontal"
            android:padding="10dp" >

            <TextView
                android:id="@+id/staff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:clickable="true"
                android:hint="@string/staff"
                android:textSize="14sp"
                android:layout_margin="10dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/members"
                style="@style/btnStyleGrey"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/users2"
                android:text="@string/plus" 
                android:layout_toRightOf="@+id/staff"
                />


        </RelativeLayout>

        <Button
            android:id="@+id/submit"
            style="@style/btnStyleBlue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/submit" />
    </LinearLayout>

</ScrollView>

这是日志:

05-09 18:01:44.684: E/AndroidRuntime(1480): FATAL EXCEPTION: main
05-09 18:01:44.684: E/AndroidRuntime(1480): java.lang.NullPointerException
05-09 18:01:44.684: E/AndroidRuntime(1480):     at com.pfe.risu.ManageActivity$NewCaseFragment.onCreateView(ManageActivity.java:195)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.View.measure(View.java:12603)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.View.measure(View.java:12603)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.View.measure(View.java:12603)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.View.measure(View.java:12603)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.os.Looper.loop(Looper.java:137)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-09 18:01:44.684: E/AndroidRuntime(1480):     at dalvik.system.NativeStart.main(Native Method)

解决第二个问题:

现在我在这些行上得到空指针异常:

orgname.setVisibility(View.VISIBLE); 
orgaddress.setVisibility(View.VISIBLE);

II 在 onItemSelected 方法中初始化变量:

public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
       {


           EditText orgname = (EditText)getActivity().findViewById(R.id.orgname);
            EditText orgaddress = (EditText)getActivity().findViewById(R.id.orgaddress);

        orgname.setVisibility(View.VISIBLE);
        orgaddress.setVisibility(View.VISIBLE);

       }

我没有例外,但 ItemSelectedListner 上的似乎没有工作,并且 EditTexts 是可见的:(

更换后

EditText orgname = (EditText)getActivity(). findViewById(R.id.orgname); EditText orgaddress = (EditText)getActivity(). findViewById(R.id.orgaddress);

EditText orgname = (EditText)view.findViewById(R.id.orgname);
 EditText orgaddress = (EditText)view.findViewById(R.id.orgaddress);

ManageActivity 显示 EditTexts 是不可见的,但随后应用程序再次停止。这是日志:

    05-09 20:58:24.401: E/AndroidRuntime(581): FATAL EXCEPTION: main
05-09 20:58:24.401: E/AndroidRuntime(581): java.lang.NullPointerException
05-09 20:58:24.401: E/AndroidRuntime(581):  at com.pfe.risu.ManageActivity$NewCaseFragment.onItemSelected(ManageActivity.java:208)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:882)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.widget.AdapterView.access$200(AdapterView.java:48)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.os.Handler.handleCallback(Handler.java:605)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.os.Looper.loop(Looper.java:137)
05-09 20:58:24.401: E/AndroidRuntime(581):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-09 20:58:24.401: E/AndroidRuntime(581):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:58:24.401: E/AndroidRuntime(581):  at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:58:24.401: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-09 20:58:24.401: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-09 20:58:24.401: E/AndroidRuntime(581):  at dalvik.system.NativeStart.main(Native Method)

这是第 208 行:

orgname.setVisibility(View.VISIBLE);
4

1 回答 1

0

你忘了初始化你的变量

EditText orgname;        
EditText orgaddress;
Button members;

所以当你在做

members.setOnClickListener(membersListener);
orgname.setVisibility(View.VISIBLE);
orgaddress.setVisibility(View.VISIBLE);

抛出ANullPointerException并且您的应用停止

编辑 :

您必须使用view您膨胀的变量来获取变量:

代替 :

Button members = (Button)getActivity(). findViewById(R.id.members);
EditText orgname = (EditText)getActivity(). findViewById(R.id.orgname);
EditText orgaddress = (EditText)getActivity(). findViewById(R.id.orgaddress);
Spinner spinner1 = (Spinner)getActivity(). findViewById(R.id.spinner1);

经过 :

Button members = (Button)rootView.findViewById(R.id.members);
EditText orgname = (EditText)rootView.findViewById(R.id.orgname);
EditText orgaddress = (EditText)rootView.findViewById(R.id.orgaddress);
Spinner spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);

编辑 :

代替:

EditText orgname = (EditText)getActivity().findViewById(R.id.orgname);
 EditText orgaddress = (EditText)getActivity().findViewById(R.id.orgaddress);

经过 :

EditText orgname = (EditText)view.findViewById(R.id.orgname);
EditText orgaddress = (EditText)view.findViewById(R.id.orgaddress);
于 2013-05-09T17:37:36.927 回答