在null对象引用上的android.view.View android.view.View.findViewById(int)'

时间:2017-12-05 06:37:31

标签: java android nullpointerexception

我有一个问题。当我编译时总是得到像下面的错误。所以我希望有人可以帮助理解这个问题。当我打开活动时总是堆叠......请帮助我。

SettingAct.JAVA

package com.solusiwebmaster.antrian;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.Toast;
import com.solusiwebmaster.antrian.SettingAct_ViewHelper.SettingActViewInterface;
import tools.AntrianConfig;
import tools.AntrianHelper;
import tools.AntrianHelper.AntrianChanged;

public class SettingAct extends Activity {
    AntrianHelper antrianHelper;
    AntrianConfig antrianconfig;
    EditText et_antrian;
    EditText et_loket;
    LayoutInflater inflater;
    private Boolean settingchanged = Boolean.valueOf(false);
    SettingActViewInterface settingviewInterface = new C06411();
    SharedPreferences sharedpref;
    TableLayout table;
    SettingAct_ViewHelper viewhelpersetting;


    class C06411 implements SettingActViewInterface {
        C06411() {
        }

        public void setServerPort(int port) {
            if (SettingAct.this.antrianconfig.port != port && (port+"").length() >= 4) {
                SettingAct.this.antrianconfig.port = port;
                SettingAct.this.settingchanged = Boolean.valueOf(true);
            }
        }

        public void setServerAddress(String s) {
            if (!SettingAct.this.antrianconfig.serverAddress.equals(s)) {
                SettingAct.this.antrianconfig.serverAddress = s;
                SettingAct.this.settingchanged = Boolean.valueOf(true);
            }
        }

        public void setPlaySoundAt(int i) {
            SettingAct.this.antrianconfig.playSoundAt = i;
            SettingAct.this.settingchanged = Boolean.valueOf(true);
        }

        public void setLoket(int loket) {
            if (SettingAct.this.antrianconfig.loket != loket) {
                SettingAct.this.antrianconfig.loket = loket;
                SettingAct.this.settingchanged = Boolean.valueOf(true);
            }
        }

        public void setAppMode(int i) {
            System.out.println("apasih appmodenya : " + i + "servernya " + AntrianConfig.M_SERVER);
            SettingAct.this.antrianconfig.appMode = i;
            SettingAct.this.settingchanged = Boolean.valueOf(true);
            SettingAct.this.viewhelpersetting.addAllsettingChild();
        }

        public void setAntrian(int _antrian) {
            SettingAct.this.antrianHelper.setAntrian(_antrian);
            SettingAct.this.settingchanged = Boolean.valueOf(true);
        }

        public int getServerPort() {
            return SettingAct.this.antrianconfig.port;
        }

        public String getServerAddress() {
            String add = SettingAct.this.antrianconfig.serverAddress;
            if (add == null) {
                return "";
            }
            return add;
        }

        public int getPlaysoundAt() {
            return SettingAct.this.antrianconfig.playSoundAt;
        }

        public int getLoket() {
            return SettingAct.this.antrianconfig.loket;
        }

        public int getCurrentAntrian() {
            return SettingAct.this.antrianHelper.getAntrian();
        }

        public int getAppMode() {
            return SettingAct.this.antrianconfig.appMode;
        }
    }

    class C06422 implements AntrianChanged {
        C06422() {
        }

        public void warning(String msg) {
            Toast.makeText(SettingAct.this, msg, 0).show();
        }

        public void antrianChanged() {
            if (SettingAct.this.et_antrian != null) {
                SettingAct.this.et_antrian.setText(new StringBuilder(String.valueOf(SettingAct.this.antrianHelper.getAntrian())).toString());
            }
            if (SettingAct.this.et_loket != null) {
                SettingAct.this.et_loket.setText(new StringBuilder(String.valueOf(SettingAct.this.antrianHelper.antrianConfig.loket)).toString());
            }
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        this.table = (TableLayout) findViewById(C0419R.id.as_table);
        this.inflater = getLayoutInflater();
        this.sharedpref = getSharedPreferences(AntrianConfig.SHARED_PREFFILE, 0);
        this.antrianconfig = new AntrianConfig(this.sharedpref);
        this.antrianHelper = new AntrianHelper(this, this.sharedpref);
        this.antrianHelper.setAntrianChangedCallback(new C06422());
        this.antrianHelper.loadCurrentAntrian();
        this.viewhelpersetting = new SettingAct_ViewHelper(this, this.settingviewInterface);
        this.viewhelpersetting.addAllsettingChild();
    }

    protected void onDestroy() {
        if (this.settingchanged.booleanValue()) {
            this.antrianconfig.saveConfig();
            try {
                ActBroadcastReceiver.sendSettingChanged(this);
            } catch (Exception e) {
            }
        }
        super.onDestroy();
    }

    public static void goHere(Activity actIn) {
        actIn.startActivity(new Intent(actIn, SettingAct.class));
    }
}

SettingAct_ViewHelper.java

    package com.solusiwebmaster.antrian;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import server.AntrianResponse;
import server.ParamRequest;
import tools.AntrianConfig;
import tools.MyClient;
import tools.MyClient.MyClientCallback;
import tools.NetWorkTools;
import tools.ResourceTools;
import uihelper.SmallTombolBulat;
import uihelper.TombolSwitch;

public class SettingAct_ViewHelper {
    Activity act;
    EditText et_antrian;
    EditText et_loket;
    SettingActViewInterface ifc;
    MyClient oldclient;

    LayoutInflater inflater = this.act.getLayoutInflater();
    TableLayout table = ((TableLayout) this.act.findViewById(R.id.as_table));

    TombolSwitch tombolstatus;
    public SettingAct activity;


    class C04201 implements OnKeyListener {
        C04201() {
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == 1) {
                try {
                    SettingAct_ViewHelper.this.ifc.setLoket(Integer.parseInt(((EditText) v).getText().toString()));
                } catch (Exception e) {
                }
            }
            return false;
        }
    }

    class C04212 implements OnKeyListener {
        C04212() {
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == 1) {
                try {
                    SettingAct_ViewHelper.this.ifc.setAntrian(Integer.parseInt(((EditText) v).getText().toString()));
                } catch (Exception e) {
                }
            }
            return false;
        }
    }

    class C04223 implements OnItemSelectedListener {
        C04223() {
        }

        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            if (position != SettingAct_ViewHelper.this.ifc.getAppMode()) {
                SettingAct_ViewHelper.this.ifc.setAppMode(position);
            }
        }

        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    class C04234 implements OnKeyListener {
        C04234() {
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            try {
                if (event.getAction() == 1) {
                    EditText et = (EditText) v;
                    String text = et.getText().toString();
                    if (!text.startsWith("http://")) {
                        text = "http://" + text;
                        et.setText(text);
                        et.setSelection(text.length());
                    }
                    SettingAct_ViewHelper.this.ifc.setServerAddress(text);
                }
            } catch (Exception e) {
            }
            return false;
        }
    }

    class C04245 implements OnItemSelectedListener {
        C04245() {
        }

        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            int psoundat = position == 0 ? AntrianConfig.M_CLIENT : AntrianConfig.M_SERVER;
            if (psoundat != SettingAct_ViewHelper.this.ifc.getPlaysoundAt()) {
                SettingAct_ViewHelper.this.ifc.setPlaySoundAt(psoundat);
                SettingAct_ViewHelper.this.addAllsettingChild();
            }
        }

        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    class C04256 implements OnKeyListener {
        C04256() {
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            try {
                if (event.getAction() == 1) {
                    EditText et = (EditText) v;
                    if (et.getText().toString().length() >= 4) {
                        SettingAct_ViewHelper.this.ifc.setServerPort(Integer.parseInt(et.getText().toString()));
                    }
                }
            } catch (Exception e) {
            }
            return false;
        }
    }

    class C04267 implements OnClickListener {
        C04267() {
        }

        public void onClick(View v) {
            Toast.makeText(SettingAct_ViewHelper.this.act, "Tutup dan buka aplikasi untuk menyalakan server", 0).show();
        }
    }

    public interface SettingActViewInterface {
        int getAppMode();

        int getCurrentAntrian();

        int getLoket();

        int getPlaysoundAt();

        String getServerAddress();

        int getServerPort();

        void setAntrian(int i);

        void setAppMode(int i);

        void setLoket(int i);

        void setPlaySoundAt(int i);

        void setServerAddress(String str);

        void setServerPort(int i);
    }

    class C06438 implements MyClientCallback {
        C06438() {
        }

        public void result(int i) {
            if (i == 200) {
                SettingAct_ViewHelper.this.tombolstatus.setOn(Boolean.valueOf(true));
            } else {
                SettingAct_ViewHelper.this.tombolstatus.setOn(Boolean.valueOf(false));
            }
        }

        public void error(String msg) {
        }
    }

    public SettingAct_ViewHelper(Activity actIn, SettingActViewInterface ifcIn) {
        this.act = actIn;
        this.ifc = ifcIn;
        ((TextView) this.act.findViewById(R.id.as_title)).setText(new StringBuilder(String.valueOf(this.act.getResources().getString(R.string.app_name))).append(" v").append(ResourceTools.getVersionname(this.act)).toString());
    }

    public void addAllsettingChild() {
        this.table.removeAllViews();
        addSettingChild();
        addClientMenu();
        addServerMenu();
    }

    class ChildView {
        public View rootview=SettingAct_ViewHelper.this.inflater.inflate(R.layout.activity_setting_child, SettingAct_ViewHelper.this.table, false);
        public LinearLayout childlinear = ((LinearLayout) this.rootview.findViewById(R.id.asc_childLinear));
        public TextView textview = ((TextView) this.rootview.findViewById(R.id.asc_text));
        public SmallTombolBulat tombol = ((SmallTombolBulat) this.rootview.findViewById(R.id.asc_tombol));

        public ChildView(int iconcode, String textlabel, int circleColor) {
            this.rootview = SettingAct_ViewHelper.this.inflater.inflate(R.layout.activity_setting_child, SettingAct_ViewHelper.this.table, false);
            try {
                this.tombol.setIconCode(SettingAct_ViewHelper.this.act.getResources().getString(iconcode));
            } catch (Exception e) {
            }
            this.textview.setText(textlabel);
            this.tombol.setCircleColor(circleColor);
        }
    }

    private void addSettingChild() {
        addEditextChild(R.string.im_laptop, "Loket", this.ifc.getLoket()+"", Color.parseColor("#e63e6f"), new C04201());
        addEditextChild(R.string.im_user, "Antrian", this.ifc.getCurrentAntrian()+"", Color.parseColor("#730679"), new C04212());
        ChildView modechild = new ChildView(R.string.im_tree, "Mode", Color.parseColor("#99c210"));
        String[] modeopt = new String[]{"Standalone", "Client", "Server"};
        Spinner spinner = new Spinner(this.act);
        spinner.setAdapter(new ArrayAdapter(this.act, 17367048, modeopt));
        spinner.setSelection(this.ifc.getAppMode());
        spinner.setOnItemSelectedListener(new C04223());
        modechild.childlinear.addView(spinner);
        this.table.addView(modechild.rootview);
    }

    private void addClientMenu() {
        int psound = 0;
        if (this.ifc.getAppMode() == AntrianConfig.M_CLIENT) {
            String serveradd = this.ifc.getServerAddress();
            if (serveradd == null) {
                serveradd = "";
            }
            addEditextChild(R.string.im_podcast, "Server", serveradd, Color.parseColor("#ffab31"), 1, new C04234());
            ChildView modechild = new ChildView(R.string.im_megaphone, "Speakers", Color.parseColor("#99c210"));
            String[] modeopt = new String[]{"Client", "Server"};
            Spinner spinner = new Spinner(this.act);
            spinner.setAdapter(new ArrayAdapter(this.act, 17367048, modeopt));
            if (this.ifc.getPlaysoundAt() != 1) {
                psound = 1;
            }
            spinner.setSelection(psound);
            spinner.setOnItemSelectedListener(new C04245());
            modechild.childlinear.addView(spinner);
            this.table.addView(modechild.rootview);
        }
    }

    private void addServerMenu() {
        if (Integer.valueOf(this.ifc.getAppMode()).equals(Integer.valueOf(AntrianConfig.M_SERVER))) {
            addEditextChild(R.string.im_podcast, "Port", new StringBuilder(String.valueOf(this.ifc.getServerPort())).toString(), Color.parseColor("#730679"), new C04256());
            String devip = NetWorkTools.getIpAddress(this.act);
            if (devip == null) {
                devip = "";
            }
            devip = "http://" + devip + ":" + this.ifc.getServerPort() + "/";
            ChildView ipcv = new ChildView(R.string.im_podcast, "Server URL", Color.parseColor("#99c210"));
            TextView tv = new TextView(this.act);
            tv.setTextSize(2, 16.0f);
            tv.setTextColor(ViewCompat.MEASURED_STATE_MASK);
            tv.setGravity(16);
            tv.setText(devip);
            ipcv.childlinear.addView(tv);
            this.table.addView(ipcv.rootview);
            ChildView cv = new ChildView(R.string.im_podcast, "Server Status", Color.parseColor("#99c210"));
            this.tombolstatus = new TombolSwitch(this.act);
            this.tombolstatus.setOn(Boolean.valueOf(false));
            cv.childlinear.addView(this.tombolstatus);
            this.table.addView(cv.rootview);
            this.tombolstatus.setOnClickListener(new C04267());
            getStatusServer();
        }
    }

    private void addEditextChild(int icon, String textlabel, String valtext, int circleColor, OnKeyListener keylistener) {
        addEditextChild(icon, textlabel, valtext, circleColor, 2, keylistener);
    }

    private void addEditextChild(int icon, String textlabel, String valtext, int circleColor, int editortype, OnKeyListener keylistener) {
        ChildView cvLoket = new ChildView(icon, textlabel, circleColor);
        EditText loketeditext = new EditText(this.act);
        loketeditext.setText(valtext);
        loketeditext.setInputType(editortype);
        loketeditext.setOnKeyListener(keylistener);
        cvLoket.childlinear.addView(loketeditext);
        if (textlabel.equals("Loket")) {
            this.et_loket = loketeditext;
        }
        if (textlabel.equals("Antrian")) {
            this.et_antrian = loketeditext;
        }
        this.table.addView(cvLoket.rootview);
    }

    private void getStatusServer() {
        try {
            this.oldclient.disconnect();
        } catch (Exception e) {
        }
        if (this.tombolstatus != null) {
            ParamRequest param = new ParamRequest();
            param.allowresponse = Boolean.valueOf(true);
            param.reqmode = AntrianResponse.REQMODE_TEST;
            this.oldclient = MyClient.getAntrian(this.act, new C06438(), "http://127.0.0.1:" + this.ifc.getServerPort(), param);
        }
    }
}

FATAL

FATAL EXCEPTION: main
Process: com.solusiwebmaster.antrian, PID: 22033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.solusiwebmaster.antrian/com.solusiwebmaster.antrian.SettingAct}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.LayoutInflater android.app.Activity.getLayoutInflater()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.LayoutInflater android.app.Activity.getLayoutInflater()' on a null object reference
at com.solusiwebmaster.antrian.SettingAct_ViewHelper.<init>(SettingAct_ViewHelper.java:37)
at com.solusiwebmaster.antrian.SettingAct.onCreate(SettingAct.java:128)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 

当我运行应用程序时总是显示FATAL EXCEPTION:main。请帮忙

1 个答案:

答案 0 :(得分:1)

首先初始化rootview

class ChildView {
    public View rootview=SettingAct_ViewHelper.this.inflater.inflate(R.layout.activity_setting_child, SettingAct_ViewHelper.this.table, false);
    public LinearLayout childlinear = ((LinearLayout) this.rootview.findViewById(R.id.asc_childLinear));       
    public TextView textview = ((TextView) this.rootview.findViewById(R.id.asc_text));
    public SmallTombolBulat tombol = ((SmallTombolBulat) this.rootview.findViewById(R.id.asc_tombol));