如果配置更改,则为空对象引用

时间:2015-10-09 23:10:13

标签: android orientation android-linearlayout android-view onconfigurationchanged

如果只对LinearLayout个对象进行配置更改,我会得到空对象引用。

风景模式除了一个视图外,我必须setVisiblity = View.GONE查看所有视图

人像模式我必须setVisibility = View.VISIBLE查看所有观看次数:

if (Activity started in portrait mode then turned into landscape) 
{it work fine}
else {it get null object reference only for linearLayout objects}

对象返回此错误称为chartprogress_layout

编辑

大多数课程"相关代码段"导致全班1000行无法发布

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
    }

清单

 <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文件

    <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>

提前完成!

0 个答案:

没有答案