Activity start的Java Nullpointer异常

时间:2014-07-25 02:08:42

标签: android android-intent android-activity

我正在创建一个要显示的AlertDialog,我正在创建一个活动。这是完整的代码,清单和错误跟踪。任何人都可以说我正在犯的错误。

// From a service
Intent intent = new Intent( this, Myclass.class  );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra("TYPE", "ASToContainer");
startActivity( intent );

// Activity
public class Myactivity extends Activity
{
@Override
public void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );

    String type = savedInstanceState.getString("TYPE");

    if( type.equalsIgnoreCase("ASToContainer"))
    {
        AlertDialog.Builder builder = new AlertDialog.Builder( Myactivity.this );
        builder.setMessage( R.string.SWITCH_ACTIVESYNC_TO_WORKSPACE );
        builder.setIcon( R.drawable.icon );
        builder.setCancelable( false );
        builder.setTitle( R.string.SWITCH_AS_CONFIRMATION);
        builder.setPositiveButton( R.string.DIALOG_YES, new DialogInterface.OnClickListener() 
        {
            public void onClick( DialogInterface dialog, int which ) 
            {
                AnotherService serviceInstance = AnotherService.getInstance();
                if( serviceInstance != null )
                {
                    serviceinstance._SwitchASToContainerRejected = 1;
                    serviceInstance.requestClearActiveSyncRegistration();
                }
            }           
        } )
        .setNegativeButton( R.string.DIALOG_NO, new DialogInterface.OnClickListener() 
        {
            public void onClick( DialogInterface dialog, int which ) 
            {
                AnotherService.getInstance()._SwitchASToContainerRejected = 0;      
            }           
        } );
        AlertDialog alert = builder.create();
        alert.show();
    }
}
}

// Manifest
<android:name="mypackagename.activity.Myactivity" android:theme="@android:style/Theme.Dialog"></activity>

// trace


07-24 16:30:04.877: C/Myapp(619): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.example.myapp/com.example.myapp.IMC}: java.lang.NullPointerException
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 16:30:04.877: C/Myapp(619):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 16:30:04.877: C/Myapp(619):  at android.os.Looper.loop(Looper.java:123)
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 16:30:04.877: C/Myapp(619):  at java.lang.reflect.Method.invokeNative(Native Method)
07-24 16:30:04.877: C/Myapp(619):  at java.lang.reflect.Method.invoke(Method.java:521)
07-24 16:30:04.877: C/Myapp(619):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 16:30:04.877: C/Myapp(619):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 16:30:04.877: C/Myapp(619):  at dalvik.system.NativeStart.main(Native Method)
07-24 16:30:04.877: C/Myapp(619): Caused by: java.lang.NullPointerException
07-24 16:30:04.877: C/Myapp(619):  at com.example.myapp.IMC.onCreate(IMC.java:57)
07-24 16:30:04.877: C/Myapp(619):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-24 16:30:04.877: C/Myapp(619):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我做错了什么?这是创建Dialog提示符的正确方法吗?请帮帮我

1 个答案:

答案 0 :(得分:0)

问题是&#34;活动名称&#34;。

Intent intent = new Intent(this,Myclass.class); //你用过

但你正在使用&#34; Myactivity&#34;

从服务中,您正在呼叫活动名称&#34; Myclass&#34;但你的活动名称是&#34; Myactivity&#34;。这是为什么它会抛出Nullpointerexception。因为它无法找到课程/活动名称&#34; Myclass&#34;。

因此将Myclass名称更改为Myactivity。它将解决问题。