Null object reference if configuration changes

眉间皱痕 提交于 2019-12-06 02:58:23

Hi @Error, I will try with One Simple Demo in which i am able to get the Object of Progressbar and Linear Layout when Screen orientation changed. Here i have described simple example as follows:

public class MainActivity extends Activity {

float Float;
int Day = 0;
TextView tview;
int i;
int num;
static Spinner spinner;
static int pos;
boolean gool = false;

int intent;

String orientation;
LinearLayout historyText;

int First_entries;
LinearLayout chart;
LinearLayout progress_layout;
boolean HistoryTextAfterRotation;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    historyText = (LinearLayout) findViewById(R.id.historyLayoutText);
    chart = (LinearLayout) findViewById(R.id.chartid);
    progress_layout = (LinearLayout) findViewById(R.id.group);
    tview = (TextView) findViewById(R.id.chart);
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    if (getScreenOrientation() == 1) {
        chart.setVisibility(View.VISIBLE);
    } else {
        chart.setVisibility(View.GONE);
    }
}

public int getScreenOrientation() {
    Display getOrient = getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if (getOrient.getWidth() == getOrient.getHeight()) {
        orientation = Configuration.ORIENTATION_SQUARE;
    } else {
        if (getOrient.getWidth() < getOrient.getHeight()) {
            orientation = Configuration.ORIENTATION_PORTRAIT;
        } else {
            orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onConfigurationChanged(Configuration _newConfig) {
    // TODO Auto-generated method stub
    if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        chart.setVisibility(View.GONE);
    }

    if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        chart.setVisibility(View.VISIBLE);
    }
    super.onConfigurationChanged(_newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

And the Layout file as below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:weightSum="5"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/chartid"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="25dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/historyLayoutText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="History"
        android:textColor="@android:color/white"
        android:textSize="23dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:gravity="right"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="130dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dp" />
</LinearLayout>

</LinearLayout>

Please Check it out As I am able to do with this code and let me know If you have any doubts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!