2 jzk15666378163 jzk15666378163 于 2016.05.09 21:46 提问

在Fragment中,怎么点击界面的按钮就闪退呢,求大神帮忙

图片说明

9个回答

qq_31354993
qq_31354993   2016.05.09 21:51

能把报错的打印拿出来吗?

jzk15666378163
jzk15666378163 我在下面贴上了。
一年多之前 回复
jzk15666378163
jzk15666378163 嗯嗯,好的。
一年多之前 回复
jzk15666378163
jzk15666378163   2016.05.09 22:23

图片说明

q610098308
q610098308   2016.05.09 22:31

Servicei没有在AndroidMainfest.xml中声明;

jzk15666378163
jzk15666378163 我声明了啊
一年多之前 回复
jzk15666378163
jzk15666378163   2016.05.09 22:36

这是错误日志的报告,求大神帮忙。图片说明

jzk15666378163
jzk15666378163   2016.05.09 22:40

图片说明
求大神帮忙啊,很急!!!

AinUser
AinUser   2016.05.09 22:40

指定错误,空指针错误。

jzk15666378163
jzk15666378163 那应该怎么改呢
一年多之前 回复
jzk15666378163
jzk15666378163   2016.05.09 22:51

public class MessageFragment extends Fragment {
WifiManager wifi = null;
Button open = null;
Button gets = null;
Button sends = null;
Button openget = null;
// Button bind=null;
boolean flag = false;
LocalService myservice;
Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 1:
            Toast.makeText(getActivity(), msg.obj.toString(), 5000)
                    .show();
            break;
        case 2:
            Toast.makeText(getActivity(), msg.obj.toString(), 5000)
                    .show();
            break;
        default:
            break;
        }
    }

};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View newsLayout = inflater.inflate(R.layout.message_layout, container,
            false);

    return newsLayout;

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    open = (Button) view.findViewById(R.id.openheat);
    gets = (Button) view.findViewById(R.id.getphone);
    sends = (Button) view.findViewById(R.id.startsocket);


    sc = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("aaa", "绑定成功");
            LocalBinder binder = (LocalBinder) service; // 通过IBinder获取Service
            myservice = binder.getService();
            myservice.startWaitDataThread(handler);// 完成绑定后打开另外一条线程等待消息接收
        }
    };
    open.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            flag = !flag;
            setWifiApEnabled(flag);// 开启热点
        }
    });
    gets.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {// 获取连接到消息发送的手机IP地址
            ArrayList<String> connectedIP = getConnectedIP();
            StringBuilder resultList = new StringBuilder();

            for (String ip : connectedIP) {
                resultList.append(ip);
                resultList.append("\n");
            }
            Toast.makeText(getActivity(),
                    "连接到手机上的Ip是:" + resultList.toString(), 3000).show();
        }
    });

    sends.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ArrayList<String> connectedIP = getConnectedIP();
            for (String ip : connectedIP) {
                if (ip.contains(".")) {
                    new SendDataThread(ip).start();// 消息发送方启动线程发送消息
                }
            }
        }
    });
    /*
     * openget.setOnClickListener(new View.OnClickListener() {
     * 
     * @Override public void onClick(View v) {
     * 
     * } });
     */
    connection();// 绑定等待发送方消息的service

}

private ArrayList<String> getConnectedIP() {// 获取连接到本机热点上的手机ip
    ArrayList<String> connectedIP = new ArrayList<String>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(
                "/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                String ip = splitted[0];
                connectedIP.add(ip);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connectedIP;
}

public boolean setWifiApEnabled(boolean enabled) {
    if (enabled) { // disable WiFi in any case
        // wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi
        wifi.setWifiEnabled(false);
    }
    try {
        // 热点的配置类
        WifiConfiguration apConfig = new WifiConfiguration();
        // 配置热点的名称(可以在名字后面加点随机数什么的)
        apConfig.SSID = "scorewizard";
        String password = "password";
        // 配置热点的密码
        apConfig.preSharedKey = password;
        apConfig.hiddenSSID = true;
        apConfig.allowedAuthAlgorithms
                .set(WifiConfiguration.AuthAlgorithm.OPEN);
        apConfig.allowedGroupCiphers
                .set(WifiConfiguration.GroupCipher.TKIP);
        apConfig.allowedKeyManagement
                .set(WifiConfiguration.KeyMgmt.WPA_PSK);
        apConfig.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.TKIP);
        // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        apConfig.allowedGroupCiphers
                .set(WifiConfiguration.GroupCipher.CCMP);
        apConfig.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.CCMP);
        apConfig.status = WifiConfiguration.Status.ENABLED;
        // 通过反射调用设置热点
        Method method = wifi.getClass().getMethod("setWifiApEnabled",
                WifiConfiguration.class, Boolean.TYPE);
        Toast.makeText(getActivity(),
                "连接账号:" + apConfig.SSID + ",密码是:" + apConfig.preSharedKey,
                5000).show();// 提示信息接收方要连接的热点账号和密码
        // 返回热点打开状态
        return (Boolean) method.invoke(wifi, apConfig, enabled);
    } catch (Exception e) {
        return false;
    }
}

private ServiceConnection sc = null;

private void connection() {
    Intent intent = new Intent("com.deng.bindService");
    getActivity().bindService(intent, sc, getActivity().BIND_AUTO_CREATE);
}

@Override
public void onDestroy() {
    getActivity().unbindService(sc);
    super.onDestroy();
}

}
这是我写的程序,求大神指点!!!

l_vaule
l_vaule   2016.05.09 23:49

你有action是……bindService的service么

qq_28026283
qq_28026283   2016.05.10 16:45

你调试下你的onclick事件,点击事件里面有代码报空了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!