很抱歉,如果这是一个微不足道的问题,但我无法解决。我正在开发一个完整的应用程序,但我将问题简化为:
我有三个活动(已经在清单中声明),我试图用一个"外国"方法
活动1:
public void goNextActivity(View view){
Activity2 obj = new Activity2();
obj.jumpToThree(this);//I've already tried with getApplicationConext(), didn't work
}
活性2:
public void jumpToThree(Context context){
startActivity(new Intent(context, Activity3.class));
}
Activity3:
//empty standard activity that works normally
这是日志
FATAL EXCEPTION: main
Process: com.example.user.myapp, PID: 32408
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
****Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3922)****
at android.app.Activity.startActivityForResult(Activity.java:3881)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at android.app.Activity.startActivity(Activity.java:4208)
at android.app.Activity.startActivity(Activity.java:4176)
****at com.example.user.myapp.folder.Activity2.jumpToThree(Activity2.java:91)****
****at com.example.user.myapp.MainActivity.goNextActivity(MainActivity.java:26)****
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
答案 0 :(得分:1)
你绝对不能这样做:
Activity2 obj = new Activity2();
只有Android框架可以实例化Android组件(Activity
,Service
,BroadcastReceiver ,
提供商). If you instantiate an Android component using the
new`关键字,组件未正确初始化,没有它的上下文设置,并且几乎不会做任何有用的事情。
答案 1 :(得分:0)
虽然您已经实例化了Activity2对象,但是没有调用它的Activity生命周期方法,也没有完成onCreate中通常完成的超类实例化相关工作。
ContentPresenter
因此,ActivityThread只是null,因此是异常
尝试调用虚方法 “android.app.ActivityThread $ ApplicationThread null对象上的android.app.ActivityThread.getApplicationThread()' 参考