Androidruntime:FATAL EXCEPTION' s

时间:2016-09-01 21:32:47

标签: android android-layout android-studio nullpointerexception

我已经在AndroidStudio版本1.5中对我的项目进行了编码,过了一段时间我为版本2.1.2编写了相同的项目(主要是从旧项目中复制粘贴我的java方法),现在它向我显示了FATAL EXCEPTION 16错误我应该做什么摆脱这些错误。 以下是例外情况。

FATAL EXCEPTION: main
  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imtiaz.recognizer/com.example.imtiaz.recognizer.MainActivity}:java.lang.NullPointerException
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
  at android.app.ActivityThread.access$700(ActivityThread.java:168)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:177)
  at android.app.ActivityThread.main(ActivityThread.java:5493)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
  at java.lang.Class.newInstanceImpl(Native Method)
  at java.lang.Class.newInstance(Class.java:1130)
  at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
  at android.app.ActivityThread.access$700(ActivityThread.java:168)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:177)
  at android.app.ActivityThread.main(ActivityThread.java:5493)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

...
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.imtiaz.recognizer.MainActivity.<init>(MainActivity.java:26)
...

您似乎在findViewById()子类的构造函数中调用了Activity。你不能做这个。您必须使用onCreate()方法进行所有初始化。我强烈建议您了解activity lifecycle。您必须正确使用此功能才能编写任何Android应用程序。

答案 1 :(得分:0)

要修复该错误,您必须提及meissa.exe runner --resultsFilePath="pathToResults\result.trx" --outputFilesLocation="pathToBuildedFiles" --agentTag="API" --testTechnology="MSTestCore" --testLibraryPath="pathToBuildedFiles\SampleTestProj.dll" 方法本身以及类中的所有activity组件,如下所示

onCreate