问题
I get null object reference if configuration changes for only LinearLayout
objects.
landscape mode I have to setVisiblity = View.GONE
for all views except one view
portrait mode I have to setVisibility = View.VISIBLE
for all views:
if (Activity started in portrait mode then turned into landscape)
{it work fine}
else {it get null object reference only for linearLayout objects}
objects return this error are called chart
, progress_layout
.
edited
most of class "related code snippet" cause full class 1000 line cannot post it
public class Cardio extends ActionBarActivity implements InsertDialogue.Insert, saveDialogue.Save, HistoryDialogue.History, AdapterView.OnItemSelectedListener {
float Float;
int Day = 0;
Database database;
int i;
int num;
static Spinner spinner;
static int pos;
Progress p;
SubActionButton goalSubButton;
boolean gool = false;
YAxis leftAxis ;
LineChart Running_LineChart ;
int intent ;
FloatingActionMenu x ;
FloatingActionButton actionButton ;
String orientation ;
LinearLayout historyText ;
ArrayList<Entry> Running_ArrayEntry ;
int First_entries ;
LinearLayout chart ;
LinearLayout progress_layout ;
boolean HistoryTextAfterRotation ;
@Override
public 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);
Running_LineChart = (LineChart) findViewById(R.id.chart);
Log.e("oncreate","oncreate");
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
database = new Database(this);
Bundle extras = getIntent().getExtras();
intent = extras.getInt("activity");
p = new Progress();
orientation = getRotation();
if( savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.group, p)
.commit();
}
else
{
String ftagment = savedInstanceState.getString("frag");
getSupportFragmentManager().beginTransaction()
.add(p,ftagment)
.commit();
}
addPlus();
switch (orientation) {
case "l":
actionButton.setVisibility(View.GONE);
break;
case "p" :
actionButton.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
orientation = getRotation() ;
setGoalSubButton_Visiblity();
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if(actionButton!=null&&actionButton.getVisibility()!=View.GONE) {
actionButton.setVisibility(View.GONE);
}
if(spinner!=null&&spinner.getVisibility()!=View.GONE) {
spinner.setVisibility(View.GONE);
}
// p.setUserVisibleHint(false);
if(progress_layout!=null&&progress_layout.getVisibility()!=View.GONE) {
progress_layout.setVisibility(View.GONE);
}
if(chart!=null&&chart.getVisibility()!=View.GONE) {
chart.getLayoutParams().height = LinearLayout.LayoutParams.MATCH_PARENT;
}
if(historyText!=null&&historyText.getVisibility()!=View.GONE) {
historyText.setVisibility(View.GONE);
}
if(Running_LineChart!=null&&Running_LineChart.getVisibility()!=View.GONE) {
Running_LineChart.animateY(1300);
}
// Changes the height and width to the specified *pixels*
// chart = (LinearLayout) findViewById(R.id.chartid);
// chart.getLayoutParams().height = LayoutParams.MATCH_PARENT;
// setContentView(R.layout.activity_main_land);
// Toast.makeText(this,"chnge orientation",Toast.LENGTH_SHORT).show();
}
else if(newConfig.orientation ==Configuration.ORIENTATION_PORTRAIT) {
actionButton.setVisibility(View.VISIBLE);
spinner.setVisibility(View.VISIBLE);
p.setUserVisibleHint(false);
Log.e("BEFORE LOG","BEFOR PROGRESS LAYOUT ");
progress_layout.setVisibility(View.VISIBLE);
chart.getLayoutParams().height =0;
if(HistoryTextAfterRotation&&historyText.getVisibility()!=View.VISIBLE) {
historyText.setVisibility(View.VISIBLE);}
Running_LineChart.animateY(1300);
x.close(true);
}
super.onConfigurationChanged(newConfig);
}
public String getRotation(){
final int rotation = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (rotation) {
case Surface.ROTATION_0:
return "p";
case Surface.ROTATION_90:
return "l";
case Surface.ROTATION_180:
return "p";
default:
return "l";
}
}
public void setGoalSubButton_Visiblity() {
orientation = getRotation() ;
switch (orientation) {
case "p": {
if (getIdsFromGoalLimitsTables()) {
x.close(true);
actionButton.removeAllViews();
actionButton.detach();
Log.e("mohab", "gool must be visible ");
addPlus();
p.setGoal_unset();
} else {
x.close(true);
actionButton.removeAllViews();
actionButton.detach();
Log.e("mohab", "gool must be invisible");
addPlus();
p.setGoal(getgoalFromDatabase());
}
}
}
updateProgress();
addEntries(0, 0, true,false,0);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
if (p != null) {
savedInstanceState.putString("frag",
p.getTag());
}
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
setSpinner();
setGoalSubButton_Visiblity();
// addEntries(0, 0, true, false, 0); // cause its added in setGoal event
}
manifest
<activity
android:name=".Cardio.Cardio"
android:configChanges="orientation|screenSize"
>
</activity>
Logcat
10-10 07:05:42.721: E/AndroidRuntime(4115): FATAL EXCEPTION: main
10-10 07:05:42.721: E/AndroidRuntime(4115): Process: developer.mohab.gymee, PID: 4115
10-10 07:05:42.721: E/AndroidRuntime(4115): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference
10-10 07:05:42.721: E/AndroidRuntime(4115): at developer.mohab.gymee.Cardio.Cardio.onConfigurationChanged(Cardio.java:154)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:4150)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:4225)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.os.Handler.dispatchMessage(Handler.java:102)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.os.Looper.loop(Looper.java:211)
10-10 07:05:42.721: E/AndroidRuntime(4115): at android.app.ActivityThread.main(ActivityThread.java:5389)
10-10 07:05:42.721: E/AndroidRuntime(4115): at java.lang.reflect.Method.invoke(Native Method)
10-10 07:05:42.721: E/AndroidRuntime(4115): at java.lang.reflect.Method.invoke(Method.java:372)
10-10 07:05:42.721: E/AndroidRuntime(4115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
10-10 07:05:42.721: E/AndroidRuntime(4115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/layout"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical"
android:id="@+id/chartid"
android:background="@color/gridBack">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/historyLayoutText">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/start"
android:textSize="23dp"
android:layout_marginTop="5dp"
android:text="History" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/linearLayout2"
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:orientation="vertical"
android:layout_weight="1"
>
</LinearLayout>
<Spinner
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:id="@+id/spinner"
/>
</LinearLayout>
</LinearLayout>
thanx in advance!
回答1:
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.
来源:https://stackoverflow.com/questions/33048409/null-object-reference-if-configuration-changes