Android:线程退出,出现未捕获的异常 (group=0xb0f03648).nullpointerexceptio



我是安卓编程的新手。我尝试从其中一个教程中执行一个简单的应用程序,它显示运行时错误。我可以在控制台消息中看到它正在安装,但在启动时,它抛出了错误。我用实际文件名交叉验证了清单文件中的条目。它们看起来都很好。以下是来自 logcat 和活动文件的错误消息。感谢您的帮助。提前谢谢。

日志猫消息

01-02 19:54:17.179: D/AndroidRuntime(3088): Shutting down VM
01-02 19:54:17.179: W/dalvikvm(3088): threadid=1: thread exiting with uncaught exception (group=0xb0f03648)
01-02 19:54:17.239: E/AndroidRuntime(3088): FATAL EXCEPTION: main
01-02 19:54:17.239: E/AndroidRuntime(3088): java.lang.RuntimeException: Unable to start activity ComponentInfo    {com.vogella.android.intent.implicit/com.vogella.android.intent.implicit.CallIntentsActivity}: java.lang.NullPointerException
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread.performLaunchActivity    (ActivityThread.java:2211)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread.handleLaunchActivity    (ActivityThread.java:2261)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.os.Looper.loop(Looper.java:137)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at java.lang.reflect.Method.invoke(Method.java:525)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run    (ZygoteInit.java:737)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at dalvik.system.NativeStart.main(Native Method)
01-02 19:54:17.239: E/AndroidRuntime(3088): Caused by: java.lang.NullPointerException
01-02 19:54:17.239: E/AndroidRuntime(3088):     at com.vogella.android.intent.implicit.CallIntentsActivity.onCreate    (CallIntentsActivity.java:24)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.Activity.performCreate(Activity.java:5133)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.Instrumentation.callActivityOnCreate    (Instrumentation.java:1087)
01-02 19:54:17.239: E/AndroidRuntime(3088):     at android.app.ActivityThread.performLaunchActivity    (ActivityThread.java:2175)
01-02 19:54:17.239: E/AndroidRuntime(3088):     ... 11 more

下面是活动代码

package com.vogella.android.intent.implicit;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class CallIntentsActivity extends Activity {
    Spinner spinnr;
    public static final int URI_INTENT_SCHEME = 1;
    private static final int REQUEST_CODE = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        spinnr = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.intents, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnr.setAdapter(adapter);
    }
    public void onClick(View view){
        int position = spinnr.getSelectedItemPosition();
        Intent call = null;
        switch(position){
        case 0:
            call = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com"));
            break;
        case 1:
            call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:(+1)1234567899"));
            break;
        case 2:
            call = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:(+1)1234567888"));
            break;
        case 3:
            call = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:50.123,7.1434?z=19"));
            break;
        case 4:
            call = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q=query"));
            break;
        case 5:
            call = new Intent("android.media.action.IMAGE_CAPTURE");
            break;
        case 6:
            call = new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people/"));
            break;
        case 7:
            call = new Intent(Intent.ACTION_EDIT,Uri.parse("content://contacts/people/1"));
            break;
        }
        if(call!= null){
            startActivity(call);
        }
    }
    public void onActivityResult(int resultCode, int requestCode, Intent data){
        if(resultCode == RESULT_OK && requestCode == REQUEST_CODE){
            String result = data.toUri(URI_INTENT_SCHEME);
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.call_intents, menu);
        return true;
    }
}

以下是Androimanifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vogella.android.intent.implicit"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.CALL_PHONE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CAMERA" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".CallIntentsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

您没有设置contentview并且活动崩溃并出现空指针异常,在onCreate方法中,您必须设置contentView才能尝试findViewById,如下所示:

setContentView(R.layout.activity_layout);

现在您的活动中有一个实际的视图,您可以进行spinnr = (Spinner) findViewById(R.id.spinner);,请注意,当您尝试查找视图时不会发生崩溃,而是在您尝试设置适配器时发生崩溃,因为findViewById中返回的引用为 null。

如果有机会,试着读一本关于Android基础知识的好书,这样你就不会碰到简单的事情了......

希望这有帮助。

问候!

相关内容

最新更新