質問

I am new to android programming. I tried to execute a simple app from one of the tutorials and it is showing runtime error. I can see in the console messages that its getting installed but while launching, it is throwing the error. I cross verified the entries in manifest file with actual filenames. They all look fine. Below are the error messages from logcat and the activity file. Appreciate your help. Thanks in advance.

Logcat messages

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

Below is the Activity code

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;
    }

}

Below is the Androimanifest file:

<?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>
役に立ちましたか?

解決

You are not setting your contentview and the activity crashes with a null pointer exception, in the onCreate method you must set a contentView before being able to try to findViewById, something like this:

setContentView(R.layout.activity_layout);

Now that you have an actual View in your activity, you can go for spinnr = (Spinner) findViewById(R.id.spinner);, notice that the crash do not happen when trying to find the view, it happens when you tried to set the adapter, because the reference returned in findViewById is null.

Try to read a good book about Android basics if you have the chance to, so you won't bump with simple things...

Hope this Helps.

Regards!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top