2

When I want to move on on other activity I have this Exception:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ua.kbv.adviser/ua.kbv.adviser.AdviceWithText}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
    at ua.kbv.adviser.AdviceWithText.<init>(AdviceWithText.java:18)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

Manifest:

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name=".AdviceWithText"/>
    <activity android:name=".AdviceWithoutText"/>
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true"
        />
</application>

Main Activity:

public class MainActivity extends Activity{

    private static long back_pressed;

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

    public void clickOnGetAdvice(View view){


        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle(R.string.builderTitle)
                .setMessage(R.string.builderMessage)
                .setCancelable(true)
                .setIcon(R.mipmap.ic_action)
                .setNeutralButton(R.string.neutralButton, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, AdviceWithText.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.positivebutton, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, AdviceWithoutText.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();
    }

    @Override
    public void onBackPressed() {
        if (back_pressed + 2000 > System.currentTimeMillis()) {
            super.onBackPressed();
        } else {
            Toast.makeText(getBaseContext(), R.string.exitToast, Toast.LENGTH_SHORT).show();
        }

        back_pressed = System.currentTimeMillis();

    }

    public void clickOnAbout(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle(R.string.builderAboutTitle)
                .setMessage(R.string.builderMessageAbout)
                .setCancelable(true)
                .setIcon(R.mipmap.ic_action_info)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

        @Override
        protected void onDestroy () {
            super.onDestroy();
            System.exit(0); 
        }
}

Recieving Activity:

public class AdviceWithText extends Activity{

     TextView answer;
     private EditText question;
     String [] advices = getResources().getStringArray(R.array.advices);

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

    public void getAdvice(View view){
        answer = (TextView) findViewById(R.id.answerText);
        question = (EditText) findViewById(R.id.questionWith);
        String text = question.getText().toString();
        int rand = (int) Math.floor(Math.random()* advices.length);
        if(text.equals("")){
            Toast.makeText(getApplicationContext(),R.string.toastWith, Toast.LENGTH_LONG).show();
        } else if(text.matches("[0-9]+")) {
            Toast.makeText(getApplicationContext(),R.string.toastWithSecond, Toast.LENGTH_LONG).show();
        } else {
            answer.setText(advices[rand]);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    public void reset(View view){
        question = (EditText) findViewById(R.id.questionWith);
        question.setText("");
        answer.setText("");

    }

}

1 Answer 1

1

You are calling getResources() from an initializer, which will not work. In general, do not call methods on your Activity until after super.onCreate() has returned, unless directed to do so.

In this case, replace:

String [] advices = getResources().getStringArray(R.array.advices);

with:

String [] advices;

and add:

advices = getResources().getStringArray(R.array.advices);

after super.onCreate() in your onCreate() method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.