调用全局变量时适配器中的上下文问题

时间:2016-06-02 08:15:09

标签: android android-studio

我有一个全局变量,我在我的应用程序上下文中初始化,以使所有类都可以访问它。

在一个屏幕上,我有一个带适配器的列表,我想要访问名为" horizo​​ntalBoxPosition",

的变量

这是我获取数据的方式

String horitonalBoxPosition, verticalBoxPosition;

        horitonalBoxPosition = ((appContext) getContext()).getHorizontalBoxPosition();

但它输出错误:

Attempt to invoke virtual method 'java.lang.String apps.radwin.wintouch.appContext.getHorizontalBoxPosition()' on a null object reference

如何解决这个问题?尝试了许多建议,但他们没有成功。

这是我的完整适配器:

package apps.radwin.wintouch.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import apps.radwin.wintouch.Aligment_Scan_Activity;
import apps.radwin.wintouch.R;
import apps.radwin.wintouch.appContext;
import apps.radwin.wintouch.models.AligmentListModel;

/**
 * Created by shay_v on 04/05/2016.
 */
public class AligmentScanListAdapter extends ArrayAdapter<AligmentListModel> {

    public AligmentScanListAdapter(Context context, ArrayList<AligmentListModel> aligmentModel) {
        super(context, 0, aligmentModel);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        AligmentListModel aligmentModelData = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout_aligment_scan, parent, false);
        }
        // Lookup view for data population
//        TextView spliceNumberTextVew = (TextView) convertView.findViewById(R.id.list_layout_splice_textView); //casting
//
//        TextView scannedUpTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_up_textView); //casting
//        TextView scannedNormalTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_normal_textView); //casting
//        TextView scannedButtomTextView = (TextView) convertView.findViewById(R.id.list_layout_scanned_buttom_textView); //casting
//
//        TextView baseStationUpTextView = (TextView) convertView.findViewById(R.id.list_layout_base_up_textView); //casting
//        TextView baseStationNormalTextView = (TextView) convertView.findViewById(R.id.list_layout_base_normal_textView); //casting
//        TextView baseStationButtomTextView = (TextView) convertView.findViewById(R.id.list_layout_base_buttom_textView); //casting

        //Log.d ("myLogs", "drawing list");
        //lookup the views to change color later
        View leftSquare = (View) convertView.findViewById(R.id.aligmentList_LeftSquare);
        View middleSquare = (View) convertView.findViewById(R.id.aligmentList_middleSquare);
        View rightSquare = (View) convertView.findViewById(R.id.aligmentList_RightSquare);


        View leftSquareFrame = (View) convertView.findViewById(R.id.aligmentList_LeftSquare_frame);
        View middleSquareFrame = (View) convertView.findViewById(R.id.aligmentList_middleSquare_frame);
        View rightSquareFrame = (View) convertView.findViewById(R.id.aligmentList_RightSquare_frame);


        // Populate the data into the template view using the data object
//        spliceNumberTextVew.setText(aligmentModelData.cellNumber);
//
//        scannedUpTextView.setText(aligmentModelData.sectorsScannedHigh);
//        scannedNormalTextView.setText(aligmentModelData.sectorsScannedMedium);
//        scannedButtomTextView.setText(aligmentModelData.sectorsScannedLow);
//
//        baseStationUpTextView.setText(aligmentModelData.basePointsRecivedHigh);
//        baseStationNormalTextView.setText(aligmentModelData.basePointsRecivedMedium);
//        baseStationButtomTextView.setText(aligmentModelData.basePointsRecivedLow);

        if (aligmentModelData.sectorsScannedHigh.matches("true")) { //colors the left colum, the hight one
            leftSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            leftSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }

        if (aligmentModelData.sectorsScannedMedium.matches("true")) { //colors the middle colum, the hight one
            middleSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            middleSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }

        if (aligmentModelData.sectorsScannedLow.matches("true")) { //colors the low colum, the hight one
            rightSquare.setBackgroundColor(Color.argb(255, 0, 204, 102));
        } else {
            rightSquare.setBackgroundColor(Color.argb(255, 244, 224, 224));
        }


        /////////////
        //color the base stations
        int basePointsHighLength = aligmentModelData.basePointsRecivedHigh.toString().length(); // colors the rest of your basepoints found
        int basePointsRecivedMedium = aligmentModelData.basePointsRecivedMedium.toString().length();
        int basePointsRecivedLow = aligmentModelData.basePointsRecivedLow.toString().length();

        if (basePointsHighLength > 2) { //colors the left colum, the hight one
            leftSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }

        if (basePointsRecivedMedium > 2) { //colors the middle colum, the hight one
            middleSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }

        if (basePointsRecivedLow > 2) { //colors the low colum, the hight one
            rightSquare.setBackgroundColor(Color.argb(255, 255, 51, 0));
        }


        //removing the background frame
        String horitonalBoxPosition, verticalBoxPosition;

        horitonalBoxPosition = ((appContext) getContext()).getHorizontalBoxPosition();

        Log.d ("myLogs", "horizontal Position"+horitonalBoxPosition);


        return convertView;

    }


}

我的完整上下文类:

package apps.radwin.wintouch;

import android.app.Application;
import android.util.Log;

import apps.radwin.wintouch.screenManagers.AligmentManager;

/**
 * Created by shay_v on 16/05/2016.
 */

//////////////////////////////////////////////////////////////////////////////
//THIS IS THE APP CONTEXT OF THE APPLICATION - COUTION REQUIRED - HDNLE GENTLY
//////////////////////////////////////////////////////////////////////////////


public class appContext extends Application {

    private AligmentManager aligmentManagerVar = new AligmentManager(); // making aligment Manager accecible to all classes

    private String horizontalBoxPosition = "0";
    private String verticalBoxPosition = "0";


    public String getVerticalBoxPosition() {
        Log.d ("myLogs", "getsVertical: "+verticalBoxPosition);
        return verticalBoxPosition;
    }

    public void setVerticalBoxPosition(String verticalBoxPosition) {
        Log.d ("myLogs", "setsVertical: "+verticalBoxPosition);
        this.verticalBoxPosition = verticalBoxPosition;
    }

    public String getHorizontalBoxPosition() {
        return horizontalBoxPosition;
    }

    public void setHorizontalBoxPosition(String horizontalBoxPosition) {
        this.horizontalBoxPosition = horizontalBoxPosition;
    }

    public AligmentManager getAligmentManagerVar() {
        return aligmentManagerVar;
    }






}

完整错误日志:

06-02 11:12:08.091 4480-4480/apps.radwin.wintouch D/AndroidRuntime: Shutting down VM
06-02 11:12:08.101 4480-4480/apps.radwin.wintouch E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: apps.radwin.wintouch, PID: 4480
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String apps.radwin.wintouch.appContext.getHorizontalBoxPosition()' on a null object reference
                                                                        at apps.radwin.wintouch.adapters.AligmentScanListAdapter.getView(AligmentScanListAdapter.java:114)
                                                                        at android.widget.AbsListView.obtainView(AbsListView.java:2929)
                                                                        at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
                                                                        at android.widget.ListView.onMeasure(ListView.java:1212)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)
                                                                        at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1149)
                                                                        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:729)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
                                                                        at android.view.View.measure(View.java:20151)
                                                                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
                                                                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120

2 个答案:

答案 0 :(得分:0)

as + mgcaguioa用于回复将所有内容更改为静态 像魅力一样工作! 日Thnx

对于未来的用户来说,这是一个新的appcontext,向您展示我的所作所为:

package apps.radwin.wintouch;

import android.app.Application;

import apps.radwin.wintouch.screenManagers.AligmentManager;

/**
 * Created by shay_v on 16/05/2016.
 */

//////////////////////////////////////////////////////////////////////////////
//THIS IS THE APP CONTEXT OF THE APPLICATION - COUTION REQUIRED - HDNLE GENTLY
//////////////////////////////////////////////////////////////////////////////


public class appContext extends Application {

    private AligmentManager aligmentManagerVar = new AligmentManager(); // making aligment Manager accecible to all classes

    private static String horizontalBoxPosition;
    private static String verticalBoxPosition;

    public static String getVerticalBoxPosition() {
        return verticalBoxPosition;
    }

    public static void setVerticalBoxPosition(String verticalBoxPosition) {
        verticalBoxPosition = verticalBoxPosition;
    }

    public static String getHorizontalBoxPosition() {
        return horizontalBoxPosition;
    }

    public static void setHorizontalBoxPosition(String horizontalBoxPosition) {
        horizontalBoxPosition = horizontalBoxPosition;
    }

    public AligmentManager getAligmentManagerVar() {
        return aligmentManagerVar;
    }



}

答案 1 :(得分:-1)

尝试将此构造函数用于'AligmentScanListAdapter'类:

public Context con;
public AligmentScanListAdapter(Context context, ArrayList<AligmentListModel> aligmentModel) {
    super(context, 0, aligmentModel);
    this.con=context;
}

之后使用代码而不是removing the background frame代码:

 //removing the background frame
    String horitonalBoxPosition, verticalBoxPosition;

    horitonalBoxPosition = this.con.getHorizontalBoxPosition();

    Log.d ("myLogs", "horizontal Position"+horitonalBoxPosition);


    return convertView;