开始活动时出错,

时间:2013-08-06 05:27:54

标签: android

我有一个活动,其中包含指导下一个活动的按钮。

问题是包含这些按钮的第一个活动给我这个错误。

你能解释一下这个错误是什么吗?

谢谢。

08-06 05:16:53.278: E/AndroidRuntime(771): FATAL EXCEPTION: main
08-06 05:16:53.278: E/AndroidRuntime(771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lspu.handbook/com.lspu.handbook.apr}: java.lang.NullPointerException
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:137)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:5039)
08-06 05:16:53.278: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 05:16:53.278: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
08-06 05:16:53.278: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-06 05:16:53.278: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-06 05:16:53.278: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
08-06 05:16:53.278: E/AndroidRuntime(771): Caused by: java.lang.NullPointerException
08-06 05:16:53.278: E/AndroidRuntime(771):  at com.lspu.handbook.apr.onCreate(apr.java:35)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.Activity.performCreate(Activity.java:5104)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-06 05:16:53.278: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-06 05:16:53.278: E/AndroidRuntime(771):  ... 11 more

这是我的代码:

public class apr extends Activity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    this.setContentView(R.layout.apr);

    Button ar = (Button)findViewById(R.id.ar);
    ar.setOnClickListener(this);

    Button reg = (Button)findViewById(R.id.reg);
    reg.setOnClickListener(this);

    Button ta = (Button)findViewById(R.id.ta);
    ta.setOnClickListener(this);

    Button vr = (Button)findViewById(R.id.vr);
    vr.setOnClickListener(this);

    Button foreign = (Button)findViewById(R.id.foreign);
    foreign.setOnClickListener(this);

    Button non = (Button)findViewById(R.id.nonreg);
    non.setOnClickListener(this);

    Button cross = (Button)findViewById(R.id.cross);
    cross.setOnClickListener(this);

    Button scholar = (Button)findViewById(R.id.scholar);
    scholar.setOnClickListener(this);

    Button nxt = (Button)findViewById(R.id.lspu_nxt_btn);
    nxt.setOnClickListener(this);




    StartAnimations();
}
     private void StartAnimations() {
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
            anim.reset();
            LinearLayout l=(LinearLayout) findViewById(R.id.lspulayout);
            l.clearAnimation();
            l.startAnimation(anim);


            //back button
            Button backButton = (Button)findViewById(R.id.lspu_bak_btn);
            backButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent instructionsIntent = new Intent(apr.this,MainBookActivity.class);
                    startActivity(instructionsIntent);
                finish();
                }
             });

            }


     @Override
        public void onDestroy(){
            super.onDestroy();
            System.gc();
            finish();
        }

        @Override
        public void onClick(View v) {
            Intent intent = null;

            switch( v.getId() ) {

            case R.id.ar:
                intent = new Intent(this, Hand_Book_ar.class);
                break;

            case R.id.reg:
                intent = new Intent(this, Hand_Book_reg.class);
                break;

            case R.id.ta:
                intent = new Intent(this, Hand_Book_ta.class);
                break;

            case R.id.vr:
                intent = new Intent(this, Hand_Book_vr.class);
                break;

            case R.id.foreign:
                intent = new Intent(this, Hand_Book_foreign.class);
                break;

            case R.id.nonreg:
                intent = new Intent(this, Hand_Book_nonreg.class);
                break;

            case R.id.cross:
                intent = new Intent(this, Hand_Book_cross.class);
                break;

            case R.id.scholar:
                intent = new Intent(this, Hand_Book_scholar.class);
                break;

            case R.id.lspu_nxt_btn:
                intent = new Intent(this, apr2.class);
                break;


            default:
                Toast toast = Toast.makeText(this, "Do I missed the button you clicked?", Toast.LENGTH_SHORT);
                toast.show();
                break;
            }

            // Open images
            if (intent != null) {
                intent.setAction(intent.ACTION_VIEW);
                startActivity(intent);
            }
        }

         @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                //Handle the back button
                if(keyCode == KeyEvent.KEYCODE_BACK) {
                    //Ask the user if they want to quit
                    new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(R.string.quit)
                    .setMessage(R.string.really_quit)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            //Stop the activity
                            apr.this.finish();    
                        }

                    })
                    .setNegativeButton(R.string.no, null)
                    .show();

                    return true;
                }
                else {
                    return super.onKeyDown(keyCode, event);
                }

            }
}

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/menubg"
    android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



        <LinearLayout
            android:id="@+id/lspulayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/tirlw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/titleapr" />

            <Button
                android:id="@+id/ar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Admission Requirements"
    android:textSize="17dp" />


            <Button
                android:id="@+id/reg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Registration"
    android:textSize="17dp" />

            <Button
                android:id="@+id/ta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Transfer Admision"
    android:textSize="17dp" />

<Button
    android:id="@+id/vr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Validation Requirements"
    android:textSize="17dp" />

<Button
    android:id="@+id/foreign"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Foreign Student Admission"
    android:textSize="16sp" />

<Button
    android:id="@+id/nonreg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Non-Regular Admission"
    android:textSize="17sp" />

<Button
    android:id="@+id/cross"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Cross Registration"
    android:textSize="18sp" />

<Button
    android:id="@+id/scholar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/btnallctr"
    android:gravity="top|center"
    android:text="Scholarship"
    android:textSize="18sp" />
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<Button
    android:id="@+id/lspu_bak_btn"
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:layout_gravity="bottom|left"
    android:layout_marginTop="10dp"
    android:background="@drawable/backbtncntrl_hd" />

<Button
    android:id="@+id/lspu_nxt_btn"
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/lspu_bak_btn"
    android:background="@drawable/nxtbtncntrl_hd" />

</RelativeLayout>

        </LinearLayout>



    </RelativeLayout>
</LinearLayout> 

给那些帮助的人,谢谢!

1 个答案:

答案 0 :(得分:0)

at com.lspu.handbook.apr.onCreate(apr.java:35)指出按钮未初始化或换句话说它为空。在您的代码中,您正在Button方法中实例化onCreate()对象,因此一旦onCreate()返回,所有这些按钮的范围都是onCreate()的范围,这些按钮超出了范围,所以当您单击任何按钮时,您将获得NullPointerException。简单的解决方案是在方法外部创建所有按钮对象,然后在onCreate()内实例化它们。

 Button ar,reg,ta,vr,foreign,non,cross,scholar,nxt;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN);

this.setContentView(R.layout.apr);

 ar = (Button)findViewById(R.id.ar);
ar.setOnClickListener(this);

 reg = (Button)findViewById(R.id.reg);
reg.setOnClickListener(this);

 ta = (Button)findViewById(R.id.ta);
ta.setOnClickListener(this);

 vr = (Button)findViewById(R.id.vr);
vr.setOnClickListener(this);

 foreign = (Button)findViewById(R.id.foreign);
foreign.setOnClickListener(this);

 non = (Button)findViewById(R.id.nonreg);
non.setOnClickListener(this);

 cross = (Button)findViewById(R.id.cross);
cross.setOnClickListener(this);

 scholar = (Button)findViewById(R.id.scholar);
scholar.setOnClickListener(this);

 nxt = (Button)findViewById(R.id.lspu_nxt_btn);
nxt.setOnClickListener(this);




StartAnimations();

}