2011-03-19 60 views
0

那么,我的程序不断给我一个零点异常,我不知道为什么?我知道它与lvl变量有关,但我不知道是什么?我能做些什么来解决这个问题?Android程序问题,空指针EXC

的logcat:

03-18 16:14:55.852: ERROR/AndroidRuntime(277): FATAL EXCEPTION: main 
03-18 16:14:55.852: ERROR/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.think/com.games.think.Think}: java.lang.NullPointerException 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277): Caused by: java.lang.NullPointerException 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at com.games.think.Think.onCreate(Think.java:38) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-18 16:14:55.852: ERROR/AndroidRuntime(277):  ... 11 more 

下面是我的一些代码:

 package com.games.think; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 

public class Think extends Activity implements OnClickListener{ 

    int question = 1, lvl; 

    /** Called when the activity is first created. */ 
    RadioButton lvl1; 
    RadioButton lvl2; 
    RadioButton lvl3; 
    RadioButton lvl4; 
    RadioButton lvl5; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button play = (Button)findViewById(R.id.play); 
     play.setOnClickListener(this); 
     Button level = (Button)findViewById(R.id.level); 
     level.setOnClickListener(this); 
     Button exit = (Button)findViewById(R.id.exit); 
     exit.setOnClickListener(this); 
     Button setLevel = (Button)findViewById(R.id.setLevel); 
     setLevel.setOnClickListener(this); 
     lvl1 = (RadioButton)findViewById(R.id.lvl1); 
     lvl2 = (RadioButton)findViewById(R.id.lvl2); 
     lvl3 = (RadioButton)findViewById(R.id.lvl3); 
     lvl4 = (RadioButton)findViewById(R.id.lvl4); 
     lvl5 = (RadioButton)findViewById(R.id.lvl5); 

     lvl = getLevel(); 
     if(lvl == -1) { 
      lvl=getLevel(); 
     } 


    } 

    @SuppressWarnings("null") 
    private int getLevel() { 

     String FILENAME = "think_level"; 
     FileInputStream fis; 
     byte[] buffer = new byte[1000]; 



     try { 
      fis = openFileInput(FILENAME); 
     } catch (FileNotFoundException e) { 
      setLevel("1"); 
      return -1; 
     } 

     try { 
      fis.read(buffer,0,1000); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      fis.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     String level = buffer.toString(); 
     int iLevel = Integer.valueOf(level); 
     return iLevel; 
    } 

    private void setLevel(String level) { 
     String FILENAME = "think_level"; 
     String string = level; 

     FileOutputStream fos; 
     try { 
      fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
      fos.write(string.getBytes()); 
      fos.close(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 

     } 

    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
     case R.id.play: 
      setContentView(R.layout.play); 
      setQuestion(); 
     case R.id.level: 
      setContentView(R.layout.level); 
      switch(getLevel()) { 
      case 1: 
       lvl1.setChecked(true); 
      case 2: 
       lvl2.setChecked(true); 
      case 3: 
       lvl3.setChecked(true); 
      case 4: 
       lvl4.setChecked(true); 
      case 5: 
       lvl5.setChecked(true); 

      } 

     case R.id.setLevel: 
      if(lvl1.isChecked()) { 
       setLevel("1"); 
      } 
      if(lvl2.isChecked()) { 
       setLevel("2"); 
      } 
      if(lvl3.isChecked()) { 
       setLevel("3"); 
      } 
      if(lvl4.isChecked()) { 
       setLevel("4"); 
      } 
      if(lvl5.isChecked()) { 
       setLevel("5"); 
      } 







     } 
    } 

    private void setQuestion() { 




    } 



    } 
+0

在38行中抛出异常。请向我们请示#38行。 – 2011-03-19 16:27:49

+0

代码是来自Think类的代码吗?哪一行是第38行?那是NullPointer发生的地方。 – Amplify91 2011-03-19 16:29:19

+0

第38行是:setLevel.setOnClickListener(this); – 2011-03-19 16:34:47

回答

3

检查ID为“setLevel”的按钮是否在main.xml中。如果它在其他地方,你不能找到它是这样的:

Button setLevel = (Button)findViewById(R.id.setLevel); 

但你需要一个inflater。

2

如果iunderstand这是37-38线

Button setLevel = (Button)findViewById(R.id.setLevel); 
     setLevel.setOnClickListener(this); 

好像setLevel为空。是否在xml布局中描述了id为setLevel的按钮?

+0

为什么它是空的,它看起来对我来说很好? – 2011-03-19 16:37:37

+0

它可以是如果没有在您的XML文件中具有id'setLevel'的按钮。 – 2011-03-19 16:40:45

+0

你可以用布局显示xml吗? – 2011-03-19 16:41:25

2
Button setLevel = (Button)findViewById(R.id.setLevel); 

该行不返回对象。确保你的编号是正确的,并且有一个注册了它的按钮。