我遇到以下错误:任何人都可以解决此问题
调试器: AdroidRuntime:致命异常:主要 进程:com.example.avishek.wifidirect,PID:8053 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.avishek.wifidirect / com.example.avishek.wifidirect.MainActivity}:java.lang .NullPointerException:尝试调用虚拟方法'android.net.wifi.p2p.WifiP2pManager $ Channel android.net.wifi.p2p.WifiP2pManager.initialize(android.content.Context,android.os.Looper,android.net.wifi。 p2p.WifiP2pManager $ ChannelListener)'对空对象引用
MainActivity.java
中此行发生此错误 mChannel= mManager.initialize(this, Looper.getMainLooper(),null);
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/onOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="55dp"
android:text="Wifi On" />
<Button
android:id="@+id/discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/onOff"
android:layout_alignBottom="@+id/onOff"
android:layout_centerHorizontal="true"
android:text="discover" />
<ListView
android:id="@+id/peerListView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/onOff"
android:layout_marginTop="25dp"
android:background="@android:color/holo_orange_light" />
<TextView
android:id="@+id/readMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/peerListView"
android:layout_marginTop="31dp"
android:text="Message"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="italic" />
<EditText
android:id="@+id/writeMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:ems="10"
android:inputType="textPersonName"
android:layout_toStartOf="@+id/sendButton" />
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Send" />
<TextView
android:id="@+id/connectionStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Connection Status"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"
android:textSize="18sp"
android:textStyle="italic" />
MainActivity.java
package com.example.avishek.wifidirect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.net.wifi.p2p.WifiP2pManager.Channel;
public class MainActivity extends AppCompatActivity {
Button btnwifid,btnsearch, btnsend;
ListView listView;
TextView read_msg_box, ConnectionStatus;
EditText writeMsg;
WifiManager wifiManager;
WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialWork();
exqListener();
}
private void exqListener() {
btnwifid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
btnwifid.setText("ON");
}else{
wifiManager.setWifiEnabled(true);
btnwifid.setText("OFF");
}
}
});
}
private void initialWork() {
btnwifid=(Button) findViewById(R.id.onOff);
btnsearch=(Button) findViewById(R.id.discover);
btnsend=(Button) findViewById(R.id.sendButton);
listView=(ListView) findViewById(R.id.peerListView);
read_msg_box=(TextView) findViewById(R.id.readMsg);
ConnectionStatus=(TextView) findViewById(R.id.connectionStatus);
writeMsg=(EditText) findViewById(R.id.writeMsg);
wifiManager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mManager= (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
//mChannel=mManager.initialize(this,getMainLooper(),null);
mChannel= mManager.initialize(this, getMainLooper(),null);
//mChannel=mManager.initialize(this,getMainLooper(),null);
mReceiver=new WiFiDirectBroadcastReceiver(mManager,mChannel,this,this);
mIntentFilter=new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver,mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
WiFiDirectBroadcastReceiver
package com.example.avishek.wifidirect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;
import android.net.wifi.p2p.WifiP2pManager.Channel;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private Context context;
private WifiP2pManager mManager;
private Channel mChannel;
private MainActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager mManager, Channel mChannel,MainActivity ,mActivity ,Context context)
{ super();
this.mManager =mManager;
this.mChannel =mChannel;
this.mActivity=mActivity;
this.context=context;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
int state=intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);
if(state==WifiP2pManager.WIFI_P2P_STATE_ENABLED){
Toast.makeText(context,"Wifi is On", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"WiFi is Off",Toast.LENGTH_SHORT).show();}
}else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
//do
}else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
//do
}else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
//do
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.avishek.wifidirect">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
您错过了AndroidManifest.xml中的uses-feature:
<uses-feature android:name="android.hardware.wifi.direct" android:required="true"/>