“应用程序意外停止”错误

时间:2013-02-27 18:07:00

标签: android nullpointerexception runtime-error avd

我是Android开发的新手所以请耐心等待我帮助我提前解决这个问题...

当我在模拟器上运行我的应用程序时,启动画面工作正常,但在此之后,当显示下一个布局时会弹出一条消息“应用程序意外停止” 每次我运行应用程序时,相同的结果都会完美地运行,但就在错误消息之后。

我删除了&重新创建了AVD,清理了项目,重启了日食,机器却什么都没有......


如果有帮助的其他信息

在第一天,我创建了一个针对Google API 14& min SDK 8但是我在运行模拟器时遇到了麻烦,它只有一个黑屏,因此后来重新创建了AVD并将目标SDK更改为Google API 8(projrct-> properties-> Android-> Google API 8-> apply - > ok)和min SDK 4。


这是MainActivity(启动画面)

 package com.example.GPS;

 import android.app.Activity;
 import android.content.Intent;
 import android.media.MediaPlayer;
 import android.os.Bundle;

public class MainActivity extends Activity {



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

    MediaPlayer mpsplash = MediaPlayer.create(this, R.raw.startup_tone);
    mpsplash.start();

    Thread logoTimer=new Thread()
    {
        public void run()
        {           
            try
            {
                int logoTimer=0;

                while(logoTimer<3499)
                {
                    sleep(100);
                    logoTimer= logoTimer +100;
                }

                startActivity(new Intent("com.example.GPS.KCLOGO"));

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            finally
            {
                finish();
            }
        }
    };
    logoTimer.start();  
}
}

这是MenuActivity

  package com.example.GPS;

     import android.app.Activity;
     import android.content.Intent;
     import android.media.MediaPlayer;
     import android.os.Bundle;
     import android.view.View;
     import android.widget.Button;

     public class MenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

    final MediaPlayer mpButtonClick = MediaPlayer.create(this,R.raw.buttonclick);

        Button btn1 = (Button) findViewById(R.id.btnlogin);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.GPS.LOGIN"));
                mpButtonClick.start();

            }
        });

       }

       }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.GPS"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="4"
    android:targetSdkVersion="8" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.GPS.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


     <activity
        android:name="com.example.GPS.MenuActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.GPS.KCLOGO" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    //sorry didn't include it to the question at first

   <activity
        android:name="com.example.GPS.LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.GPS.LOGIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

 </application>

</manifest>

logcat的

02-27 22:29:14.031:E/AndroidRuntime(307): FATAL EXCEPTION: main
02-27 22:29:14.031:E/AndroidRuntime(307): java.lang.**RuntimeException**:Unable to start    activity ComponentInfo{com.example.GPS/com.example.GPS.MenuActivity}: java.lang.**NullPointerException**
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.os.Looper.loop(Looper.java:123)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 22:29:14.031:E/AndroidRuntime(307):at java.lang.reflect.Method.invokeNative(Native Method)
02-27 22:29:14.031:E/AndroidRuntime(307):at java.lang.reflect.Method.invoke(Method.java:521)
02-27 22:29:14.031:E/AndroidRuntime(307):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-27 22:29:14.031:E/AndroidRuntime(307):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-27 22:29:14.031:E/AndroidRuntime(307):at dalvik.system.NativeStart.main(Native Method)
02-27 22:29:14.031:E/AndroidRuntime(307): Caused by: java.lang.**NullPointerException**
02-27 22:29:14.031:E/AndroidRuntime(307):at com.example.GPS.MenuActivity.onCreate(MenuActivity.java:21)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.Instrumentation.**callActivityOnCreate**(Instrumentation.java:1047)
02-27 22:29:14.031:E/AndroidRuntime(307):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

控制台

[2013-02-27 22:28:25 - GPSTracker] Android Launch!
[2013-02-27 22:28:25 - GPSTracker] adb is running normally.
[2013-02-27 22:28:25 - GPSTracker] Performing com.example.GPS.MainActivity activity launch
[2013-02-27 22:28:25 - GPSTracker] Automatic Target Mode: launching new emulator with compatible AVD 'GMD'
[2013-02-27 22:28:25 - GPSTracker] Launching a new emulator with Virtual Device 'GMD'
[2013-02-27 22:28:27 - GPSTracker] New emulator found: emulator-5554
[2013-02-27 22:28:27 - GPSTracker] Waiting for HOME ('android.process.acore') to be launched...
[2013-02-27 22:28:50 - GPSTracker] HOME is up on device 'emulator-5554'
[2013-02-27 22:28:50 - GPSTracker] Uploading GPSTracker.apk onto device 'emulator-5554'
[2013-02-27 22:28:52 - GPSTracker] Installing GPSTracker.apk...
[2013-02-27 22:29:07 - GPSTracker] Success!
[2013-02-27 22:29:07 - GPSTracker] Starting activity com.example.GPS.MainActivity on device emulator-5554
[2013-02-27 22:29:08 - GPSTracker] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.GPS/.MainActivity }

login.xml

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

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.45" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/login_pic" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/tvuserid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="User ID"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/etuserid"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:ems="10"
                android:background="@drawable/gradiant" >

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

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/tvpass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Password"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/etpass"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="0.42"
                android:ems="10"
                android:inputType="textPassword"
                android:background="@drawable/gradiant" />

        </LinearLayout>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <Button
            android:id="@+id/btnlogin"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_weight="1"
            android:drawableLeft="@drawable/padlock"
            android:text="Login" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="150dp"
                android:layout_weight="0.04"
                android:src="@drawable/exclamation" />

            <Button
                android:id="@+id/btnsignup"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_marginRight="25dp"
                android:layout_weight="0.33"
                android:background="@color/layoutbg"
                android:text="Sign Up"
                android:textSize="16dp" />

        </LinearLayout>

     </TableRow>

  </TableLayout>

 </LinearLayout>

kclogo.xml

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

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="12dp"
    android:src="@drawable/kclogo" />

</LinearLayout>

menu.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:gravity="center_horizontal"
    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=".MainActivity"
    android:background="@color/layoutbg" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/gpscollage" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/loginsignup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:drawableLeft="@drawable/thumb_up_icon"
            android:text="Login/Sign Up" />

        <Button
            android:id="@+id/addnewtomap"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:drawableLeft="@drawable/add_to_map"
            android:text="Add Member/s To Map" />

        <Button
            android:id="@+id/addnewtocontacts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/add_contact_list"
            android:text="Add To Contact List" />
    </LinearLayout>

    </RelativeLayout>

1 个答案:

答案 0 :(得分:3)

确保布局xml中存在按钮btnlogin

您在MenuActivity中使用的布局是main.xml,您正在查找main.xml中的btnlogin,它不存在。 btn1为null,因为它在布局中不存在。