问题描述
- 在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; @Overridepublic 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(TAGhave connected success!""); Log.v(TAG###############################""); } @Override public void onNotifyWifiConnectFailed() { // TODO Auto-generated method stub Log.v(TAGhave 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""); }
解决方案
private Context mContext = null; 这个变量赋值了吗?
解决方案二:
一个空指针异常而已,程序里也没看到你对mContext赋值吗,直接看下是那个对象报空指针了
解决方案三:
null指针问题:看下属性指代,位置错误
时间: 2024-11-16 07:53:03