开发者

NullPointerException Android

开发者 https://www.devze.com 2023-04-12 06:15 出处:网络
While running the application I\'m getting these errors 10-09 10:20:57.138: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActi

While running the application I'm getting these errors

10-09 10:20:57.138: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException

10-09 10:20:57.138: ERROR/AndroidRuntime(282): Caused by: java.lang.NullPointerException

Please anyone help me to solve this problem.

Its the activity class

public class LatihanActivity extends Activity{
    private RadioButton radioButton;
    private TextView quizQuestion;  
    
    private int rowIndex = 1;
    private int questNo=0;
    private boolean ch开发者_开发技巧ecked=false;
    private boolean flag=true;
    
    private RadioGroup radioGroup;
    
    String[] corrAns = new String[5];
    
    final DatabaseHelper db = new DatabaseHelper(this);
    
    Cursor c1;
    Cursor c2;
    Cursor c3;

    int counter=1;
    String label;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String options[] = new String[19];
  
        final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
    
         // layout params to use when adding each radio button
         LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                    RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
    
        try {
            db.createDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        quizQuestion = (TextView) findViewById(R.id.TextView01);
                
        displayQuestion();
                     
        /*Displays the next options and sets listener on next button*/
        Button btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(btnNext_Listener);

        /*Saves the selected values in the database on the save button*/
        Button btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(btnSave_Listener);   

        c3 = db.getCorrAns();

        for (int i=0;i<=4;i++) {
            corrAns[i]=c3.getString(0);
            c3.moveToNext();
        }

        radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
         
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<radiogroup.getChildCount() ; i++) {
                    RadioButton btn = (RadioButton) radioGroup.getChildAt(i);                     
                    String text;
                              
                    if (btn.isPressed() && btn.isChecked() && questNo < 5) {
                        Log.e("corrAns[questNo]",corrAns[questNo]);
                        if (corrAns[questNo].equals(btn.getText()) && flag==true) {
                            flag=false;
                            checked = true; 
                        } else if(checked==true) {
                            flag=true;
                            checked = false;
                        }
                    }
                }   
            }
        });        
    }
          
    /*Called when next button is clicked*/
    private View.OnClickListener btnNext_Listener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            flag=true;
            checked = false;
            questNo++;
        
            if (questNo < 5) {
                c1.moveToNext();
                displayQuestion();
            }
        }

    };
    
    private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {}
    };


    private void displayQuestion()  {
        //Fetching data quiz data and incrementing on each click
        
        c1=db.getQuiz_Content(rowIndex);
        
        c2 =db.getAns(rowIndex++);
            
        quizQuestion.setText(c1.getString(0));
            
        radioGroup.removeAllViews();
        for (int i=0;i<=3;i++) {
            //Generating and adding 4 radio buttons dynamically 
            radioButton = new RadioButton(this);
            radioButton.setText(c2.getString(0));
            radioButton.setId(i);
            c2.moveToNext();
            radioGroup.addView(radioButton);
        }
        
    }
}

All these issues happened when run the application

10-09 11:13:15.128: ERROR/HierarchicalStateMachine(68): TetherMaster - unhandledMessage: msg.what=3

10-09 11:13:56.556: ERROR/AndroidRuntime(282): FATAL EXCEPTION: main

10-09 11:13:56.556: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.access$2300(ActivityThread.java:125)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:99)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): Caused by: java.lang.NullPointerException

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.rika.LatihanActivity.displayQuestion(LatihanActivity.java:198)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.rika.LatihanActivity.onCreate(LatihanActivity.java:69)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

10-09 11:13:56.556: ERROR/AndroidRuntime(282): ... 11 more


Possibility 1 : you should declare your Acttivity on your Manifest File ,

if you already did it and it still doesn't work , you should declare it with the name of the package if you activity is in another package :

<application ...>
<activity android:name="YouPackageName.YourActivity" /> // it should be on the <application> tag
</application>

and at last , Clean and RE execute your project .

Possibility 2 :

make sure that your RadioButtonGroup is defined on your main.xml layout , if it is , soo make sure that you didn't import the android.R ; you should use import YourPackage.R;

and at last Clean and rebuild your project .

NB : you didn't give the full stack trace of your NullPointerException , so we can't determine what is the exact problem in your code ,


I think you are creating database but u should open it after creating the database.

public void opendatabase() throws SQLException
{
    //Open the database
    String mypath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READONLY);

}

Well i m just guessing. if you will show your database class then it will be more helpful. Or you can visit http://androidlearningbegin.blogspot.com/2011/10/android-application-to-connect-to-your.html if you are using externally created sqlite database.

0

精彩评论

暂无评论...
验证码 换一张
取 消