OnClickListener中的错误;无法启动Activity

时间:2021-12-08 16:54:23

I created a button and I when I tap on it, it should start the Activity Wifi. When I test this code, nothing happens apart from this error:

我创建了一个按钮,当我点击它时,它应该启动Activity Wifi。当我测试此代码时,除了此错误之外没有任何反应:

02-19 08:34:33.455 10528-10528/test.sterela.com.sterelaapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

Wifi.java (the Activity where I have the button):

Wifi.java(我有按钮的Activity):

package test.sterela.com.sterelaapplication;

/**
 * Created by DB020490 on 17/02/2016.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Wifi extends Activity {
Intent myIntent=null;
@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.content_main_wifi);

    final Button button = (Button) findViewById(R.id.BouttonWifi);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Perform action on click
            myIntent = new Intent(Wifi.this, WifiActivity.class);
            startActivity(myIntent);
        }
    });
}
}

Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.sterela.com.sterelaapplication">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Accueil_Parametres"/>

    <activity
        android:name=".WifiActivity" />
</application>

</manifest>

content_main_wifi.xml (Wifi Activity layout):

content_main_wifi.xml(Wifi活动布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_height="fill_parent"
    android:layout_width="10dp"
    android:background="@android:color/white"
    android:id="@+id/ForceSignal"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp">

    <TextView
        android:id="@+id/tvWifiName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AP Name" />

    <TextView
        android:id="@+id/tvWifiMac"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:text="00:00:00:00:00:00" />

</LinearLayout>

</LinearLayout>

WifiActivity.java (the Activity I'm trying to start):

WifiActivity.java(我正在尝试的活动):

package test.sterela.com.sterelaapplication;

import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.content.Context;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;

public class WifiActivity extends Activity {

    private Button boutonRechercher;
    private ListView listeViewWifi;
    private List<WifiItem> listeWifiItem;
    private WifiAdapter wifiAdapter;
    private WifiManager wifiManager;
    private WifiBroadcastReceiver broadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_wifi);

    listeViewWifi = (ListView) findViewById(R.id.listViewWifi);
    boutonRechercher = (Button) findViewById(R.id.buttonRefresh);

    boutonRechercher.setOnClickListener (new View.OnClickListener()
    {
        public void onClick(View v)
        {
            if(wifiManager != null)
                wifiManager.startScan();
        }
    });

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    listeWifiItem = new ArrayList<WifiItem>();
    wifiAdapter = new WifiAdapter(this, listeWifiItem);
    listeViewWifi.setAdapter(wifiAdapter);

    broadcastReceiver = new WifiBroadcastReceiver();

    registerReceiver(broadcastReceiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

}

@Override
protected void onPause() {
    unregisterReceiver(broadcastReceiver);
    super.onPause();
}

@Override
protected void onResume() {
    registerReceiver(broadcastReceiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}


public WifiManager getCurrentWifiManager() {
    return wifiManager;
}

public WifiAdapter getWifiAdapter() {
    return wifiAdapter;
}

public List<WifiItem> getListeWifiItem() {
    return listeWifiItem;
}
}

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


0  

You are using layout R.layout.content_main_wifi inside Wifi activity, but there is no button in that layout file with id R.id.BouttonWifi.

您在Wifi活动中使用了布局R.layout.content_main_wifi,但该布局文件中没有ID为R.id.BouttonWifi的按钮。

I am sure you are getting a NullPointerException while running this code.

我确定你在运行这段代码时遇到了NullPointerException。

Solution: Add a button to layout R.layout.content_main_wifi with id R.id.BouttonWifi.

解决方案:添加一个按钮以布局R.layout.content_main_wifi,其ID为R.id.BouttonWifi。

#1


0  

You are using layout R.layout.content_main_wifi inside Wifi activity, but there is no button in that layout file with id R.id.BouttonWifi.

您在Wifi活动中使用了布局R.layout.content_main_wifi,但该布局文件中没有ID为R.id.BouttonWifi的按钮。

I am sure you are getting a NullPointerException while running this code.

我确定你在运行这段代码时遇到了NullPointerException。

Solution: Add a button to layout R.layout.content_main_wifi with id R.id.BouttonWifi.

解决方案:添加一个按钮以布局R.layout.content_main_wifi,其ID为R.id.BouttonWifi。