2 jzk15666378163 jzk15666378163 于 2016.05.11 14:44 提问

在Fragment中添加了一个按钮,但是一按按钮就闪退,求大神指点。

package com.example.fragmentdemo;

import com.example.fragmentdemo.MessageFragment;
import com.example.fragmentdemo.R;
import com.wifi.WifiAdmin;
import com.wifi.WifiApAdmin;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MessageFragment extends Fragment {
public static final String TAG = "MessageFragment";

private Button mBtn1, mBtn2;  

private WifiAdmin mWifiAdmin;  

private Context mContext = null;  


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

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

     mBtn1 = (Button)view.findViewById(R.id.button1);  
     mBtn2 = (Button)view.findViewById(R.id.button2);  
     mBtn1.setText("点击连接Wifi");  
     mBtn2.setText("点击创建Wifi热点");  
     mBtn1.setOnClickListener(new Button.OnClickListener() {  

         @Override  
         public void onClick(View v) {  
             // TODO Auto-generated method stub  

             mWifiAdmin = new WifiAdmin(mContext) {  

                 @Override  
                 public void myUnregisterReceiver(BroadcastReceiver receiver) {  
                     // TODO Auto-generated method stub  
                     getActivity().unregisterReceiver(receiver);  
                 }  

                 @Override  
                 public Intent myRegisterReceiver(BroadcastReceiver receiver,  
                         IntentFilter filter) {  
                     // TODO Auto-generated method stub  
                     getActivity().registerReceiver(receiver, filter);  
                     return null;  
                 }  

                 @Override  
                 public void onNotifyWifiConnected() {  
                     // TODO Auto-generated method stub  
                     Log.v(TAG, "have connected success!");  
                     Log.v(TAG, "###############################");  
                 }  

                 @Override  
                 public void onNotifyWifiConnectFailed() {  
                     // TODO Auto-generated method stub  
                     Log.v(TAG, "have connected failed!");  
                     Log.v(TAG, "###############################");  
                 }  
             };  
             mWifiAdmin.openWifi();  
             mWifiAdmin.addNetwork(mWifiAdmin.createWifiInfo("YOU_WIFI", "MM123456", WifiAdmin.TYPE_WPA));  

         }  
     });  

     mBtn2.setOnClickListener(new Button.OnClickListener() {  

         @Override  
         public void onClick(View v) {  
             // TODO Auto-generated method stub  

             WifiApAdmin wifiAp = new WifiApAdmin(mContext);  
             wifiAp.startWifiAp("\"HotSpot\"", "hhhhhh123");  
         }  
     });  

 }  


  @Override  
     public void onResume() {  
         super.onResume();  

         Log.d("Rssi", "Registered");  
     }  

     @Override  
     public void onPause() {  
         super.onPause();  

         Log.d("Rssi", "Unregistered");  



     }  

}
以下是错误日志报告:图片说明
求大神指点迷津!!!

3个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.05.11 15:03
 private Context mContext = null;  这个变量赋值了吗?
shenshucong520
shenshucong520   2016.05.11 15:04

一个空指针异常而已,程序里也没看到你对mContext赋值吗,直接看下是那个对象报空指针了

AinUser
AinUser   2016.05.11 15:49

null指针问题:看下属性指代,位置错误

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