Class.forName()一直显示错误:java.lang.NullPointerException

时间:2015-10-06 16:37:06

标签: java android json android-intent nullpointerexception

我想创建一个包含Activity.class的变量,以便我可以动态更改要使用intent调用的Activity。我希望startBtn启动意图到最后一级播放的Activity,我将在设备内部以json格式存储。但是,当我尝试使用forName()函数调用Activity类时,我一直出现错误。

主要Activity.class

 public class MainActivity extends Activity {

     Button startBtn, chooseLevelBtn;
     Class classVariable;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         startBtn = (Button) findViewById(R.id.startBtn);
         chooseLevelBtn = (Button) findViewById(R.id.chooseLevelBtn);

         try {
             /*
              * Set class Variable so that I can dynamically change the Activity
              * to be called with intent.
              * Testing the variable with Level001Activity, but app keep crashing
              * when startBtn is pressed with stack error I post below
              */
             classVariable = Class.forName("com.ed.pieces.Level001Activity");
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         }

         startBtn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent i = new Intent(getApplicationContext(),
                    classVariable);
                 startActivity(i);
             }
         });
    }
}

错误堆栈

  
    

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.lang.Class.getName()'                 在android.content.ComponentName。(ComponentName.java:129)                 在android.content.Intent。(Intent.java:4449)                 at com.example.ed.pieces.MainActivity $ 1.onClick(MainActivity.java:35)                 在android.view.View.performClick(View.java:5198)                 在android.view.View $ PerformClick.run(View.java:21147)                 在android.os.Handler.handleCallback(Handler.java:739)                 在android.os.Handler.dispatchMessage(Handler.java:95)                 在android.os.Looper.loop(Looper.java:148)                 在android.app.ActivityThread.main(ActivityThread.java:5417)                 at java.lang.reflect.Method.invoke(Native Method)                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

  

PS我会将最后一级播放的信息以json格式存储为字符串,例如“com.ed.pieces.Level001Activity”,当用户启动应用程序时,类classVariable应该包含Activity类的最后一次出场。

非常欢迎所有建议。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的例外来自onClick()方法及其创建Intent的尝试,而不是forName()来电。这是因为您的forName()电话因某种原因失败了。

此外,您不需要forName()。替换:

try {
   /*
    * Set class Variable so that I can dynamically change the Activity
    * to be called with intent.
    * Testing the variable with Level001Activity, but app keep crashing
    * when startBtn is pressed with stack error I post below
    */
    classVariable = Class.forName("com.ed.pieces.Level001Activity");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

使用:

classVariable = Level001Activity.class;

并添加适当的import以引入Level001Activity