user531069
user531069

Reputation: 995

App crash for no view found which is declared in landscape only

I created a Hello World app and added Landscape version of the xml. In portrait mode I have NO TextView but in Landscape version I added a TextView. I do a findviewbyid in java for this textView In Manifest I set the screen orientation to be landscape.

First time when I launch app: no problem. Now I press Power Off and then Power ON button and app crashes with NullPointer exception that textView is not found.

How do I make sure that app load fine in landscape mode directly without looking for resources in portrait mode?

Manifest:

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

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

Activity:

TextView tv;

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

    tv = (TextView)findViewById(R.id.tv);
    tv.setText("inLandscape");
}

Upvotes: 0

Views: 52

Answers (4)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

You can resolve this error in many ways. You can add null checking to TextView before setting text on it as @GreyBeardedGeek said.

OR, You can add checking about device orientation using getResources().getConfiguration().orientation. If its in Landscape mode only then set Text to TextView. I have tested on my device and its fine.

Here is the working code:

import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class LandscapePortraitActivity extends AppCompatActivity {

    TextView tv;

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

        tv = (TextView)findViewById(R.id.tv);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            tv.setText("inLandscape");
        }
    }
}

Hope this will help~

Upvotes: 0

Veneet Reddy
Veneet Reddy

Reputation: 2927

When you change orientation, your Activiy is destroyed and recreated. You can handle this by initialising the TextView only if the phone is in ORIENTATION_LANDSCAPE:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    if(getResources().getConfiguration().orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE) {
        // Initialize your TextView here
    } else {
        // Handle your potrait here
    }

}

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

Add :

android:configChanges="orientation|keyboardHidden"

in your Activity.

<activity android:name=".MainActivity"
    android:screenOrientation="landscape"
    android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

And override the onConfigurationChanged() in your Activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // ignore orientation/keyboard change
  super.onConfigurationChanged(newConfig);
}

For More Clarification see this Answer

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

If you don't have the TextView in portrait mode, then you'll need to change your code to:

tv = (TextView)findViewById(R.id.tv);
if(tv != null) {
   tv.setText("inLandscape");
}

I'm not sure that it's possible to only ever have it load in landscape - when you power the device back on, it probably tries to come up in portrait, and then switch to landscape. I wouldn't be surprised if that behavior were device-specific and undocumented.

Upvotes: 2

Related Questions