运行时异常:无法实例化活动组件

时间:2013-08-15 06:42:26

标签: android facebook

尝试将facebook sdk集成到Android应用程序,但我的应用程序无法启动。有日志。如何修复此运行时异常?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView pic = (ImageView) findViewById(R.id.imageView1);
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(this);
}

 08-15 11:48:21.712: E/AndroidRuntime(564): FATAL EXCEPTION: main
08-15 11:48:21.712: E/AndroidRuntime(564): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sri.z4globalsoft/com.sri.z4globalsoft.MainActivity}: java.lang.NullPointerException
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.os.Looper.loop(Looper.java:137)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-15 11:48:21.712: E/AndroidRuntime(564):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 11:48:21.712: E/AndroidRuntime(564):  at java.lang.reflect.Method.invoke(Method.java:511)
08-15 11:48:21.712: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 11:48:21.712: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 11:48:21.712: E/AndroidRuntime(564):  at dalvik.system.NativeStart.main(Native Method)
08-15 11:48:21.712: E/AndroidRuntime(564): Caused by: java.lang.NullPointerException
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
08-15 11:48:21.712: E/AndroidRuntime(564):  at com.sri.z4globalsoft.MainActivity.<init>(MainActivity.java:23)
08-15 11:48:21.712: E/AndroidRuntime(564):  at java.lang.Class.newInstanceImpl(Native Method)
08-15 11:48:21.712: E/AndroidRuntime(564):  at java.lang.Class.newInstance(Class.java:1319)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
08-15 11:48:21.712: E/AndroidRuntime(564):  ... 11 more

尝试通过更改下面给出的答案的代码ap然后也有问题

08-15 12:25:00.800: E/AndroidRuntime(712): FATAL EXCEPTION: main
08-15 12:25:00.800: E/AndroidRuntime(712): java.lang.NoClassDefFoundError: com.facebook.android.Facebook
08-15 12:25:00.800: E/AndroidRuntime(712):  at com.sri.z4globalsoft.MainActivity.<init>(MainActivity.java:25)
08-15 12:25:00.800: E/AndroidRuntime(712):  at java.lang.Class.newInstanceImpl(Native Method)
08-15 12:25:00.800: E/AndroidRuntime(712):  at java.lang.Class.newInstance(Class.java:1319)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.os.Looper.loop(Looper.java:137)
08-15 12:25:00.800: E/AndroidRuntime(712):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-15 12:25:00.800: E/AndroidRuntime(712):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 12:25:00.800: E/AndroidRuntime(712):  at java.lang.reflect.Method.invoke(Method.java:511)
08-15 12:25:00.800: E/AndroidRuntime(712):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 12:25:00.800: E/AndroidRuntime(712):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 12:25:00.800: E/AndroidRuntime(712):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

听起来您可能在技术上准备好之前尝试使用getResources()

如果你有这个:

String APP_ID = getResources().getString(R.string.APP_ID);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
}

请改为:

String APP_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    APP_ID = getResources().getString(R.string.APP_ID);
    ...
}

答案 1 :(得分:0)

08-15 11:48:21.712: E/AndroidRuntime(564): Caused by: java.lang.NullPointerException
08-15 11:48:21.712: E/AndroidRuntime(564):  at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
08-15 11:48:21.712: E/AndroidRuntime(564):  at com.sri.z4globalsoft.MainActivity.<init>(MainActivity.java:23)

您正尝试初始化初始化使用this作为Context的成员变量。在这种情况下,它是getResources()电话。您只能在活动生命周期的Activity或更高版本中将Context用作onCreate()

具体而言(来自问题评论),请更改:

String APP_ID = getResources().getString(R.string.APP_ID);

类似于:

String APP_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    APP_ID = getResources().getString(R.string.APP_ID);
    //...