0

I wrote the app that has two level first and second. When i choose the second level i have the exception and my app crashes.

QuestionActivity

                     package com.example.Quiz;

    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;



public class QuestionActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_activity);
        FragmentManager fm = getFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
        if (fragment == null) {
            fragment = new QuestionFragment();
            fm.beginTransaction()
                    .add(R.id.fragmentContainer, fragment)
                    .commit();
        }
    }
}

QuizActivity

package com.example.Quiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class QuizActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private Button mFirstLevelButton;
    private Button mSecondLevelButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);



        mFirstLevelButton = (Button)findViewById(R.id.first_level_button);
        mFirstLevelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(QuizActivity.this,MainActivity.class);
                startActivity(i);
            }
        });

        mSecondLevelButton = (Button)findViewById(R.id.second_level_button);
        mSecondLevelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(QuizActivity.this,com.example.Quiz.QuestionActivity.class);
                startActivity(i);

            }
        });
    }
}`

MainActivity

package com.example.Quiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private  Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question_oceans, true),
            new TrueFalse(R.string.question_mideast, false),
            new TrueFalse(R.string.question_africa, false),
            new TrueFalse(R.string.question_americas, true),
            new TrueFalse(R.string.question_asia, true),
    };
    private int mCurrentIndex = 0;

    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getQuestion();
        mQuestionTextView.setText(question);
    }
    private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
        int messageResId = 0;
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
                .show();



    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });


        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(true);

            }
        });
        mFalseButton = (Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(false);
            }
        });
        mNextButton = (Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });
        updateQuestion();
    }


}

My Manifest file

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.Quiz"
              android:versionCode="1"
              android:versionName="1.0">
        <uses-sdk android:minSdkVersion="19"/>
        <application
                android:theme = "@android:style/Theme.Translucent"
                android:label="@string/app_name" android:icon="@drawable/ic_launcher">

            <activity android:name="com.example.Quiz.QuizActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>

                </intent-filter>

            </activity>
            <activity android:name="com.example.Quiz.QuestionActivity"></activity>
            <activity android:name="com.example.Quiz.MainActivity"></activity>


        </application>
    </manifest>

Log

07-29 03:47:48.638    1229-1229/com.example.Quiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.Quiz, PID: 1229
    java.lang.NoClassDefFoundError: com.example.Quiz.QuestionActivity
            at com.example.Quiz.QuizActivity$2.onClick(QuizActivity.java:39)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

But i don't know how to fix it.

2 Answers2

0

Try to change to:

   Intent i = new Intent(v.getContext(), QuestionActivity.class);
yshahak
  • 4,996
  • 1
  • 31
  • 37
0

you can't call fragment like this

 Intent i = new Intent(QuizActivity.this,com.example.Quiz.QuestionActivity.class);
            `startActivity`(i);

try like this

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

fragment.<specific_function_name>(); 

ref fragment

Community
  • 1
  • 1
souttab
  • 696
  • 6
  • 12