简体   繁体   English

导航到其他活动时,应用程序崩溃

[英]app crashes when on navigating to different activity

I am trying to have my app navigate to another activity on a button click but whenever I do that the app simply crashes. 我试图让我的应用程序在单击按钮时导航到另一个活动,但是每当我这样做时,该应用程序都会崩溃。 I don't understand why this is happening. 我不明白为什么会这样。

My code in the add.java page which will handle the button click and navigate to the other class, WifiConnections 我在add.java页面中的代码将处理按钮单击并导航到另一个类WifiConnections

Code in add.java add.java中的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    addListenerOnButton();

}

public void addListenerOnButton() {

final Context context = this;

Button button = (Button) findViewById(R.id.addb);

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, WifiConnections.class);
        startActivity(intent);

    }

});

} }

However When I set the Wificonnections.xml to be the starting layout in the manifest file the wifi connections part works fine but on trying to navigate it crashes. 但是,当我将Wificonnections.xml设置为清单文件中的开始布局时,wifi连接部分可以正常工作,但在尝试导航时会崩溃。

Android Manifest Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abhi.myapplication2">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".login"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".WifiConnections" >
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".add" >
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".signup" >
        </activity>
    </application>

</manifest>

WifiConnections Oncreate method WifiConnections Oncreate方法

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.wifi_connections);
        mainText = (TextView) findViewById(R.id.mainText);

        // Initiate wifi service manager
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        // Check for wifi is disabled
        if (mainWifi.isWifiEnabled() == false)
        {
            // If wifi disabled then enable it
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
                    Toast.LENGTH_LONG).show();

            mainWifi.setWifiEnabled(true);
        }

        // wifi scaned value broadcast receiver
        receiverWifi = new WifiReceiver();

        // Register broadcast receiver
        // Broacast receiver will automatically call when number of wifi connections changed
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mainWifi.startScan();
        mainText.setText("Starting Scan...");
    }

Code for add.xml add.xml的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout_add">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addb"
        style="@android:style/Widget.Material.Button.Borderless"
        android:background="@drawable/add"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="170dp"
        android:onClick="addclick"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No device added"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="130dp"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to add device"
        android:id="@+id/textView2"
        android:layout_alignTop="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:textSize="20sp" />
</RelativeLayout>
    </ScrollView>

The complete project http://www37.zippyshare.com/v/10112230/file.html 完整的项目 http://www37.zippyshare.com/v/10112230/file.html

LogCat here : LogCat在这里:

12-25 09:28:10.460 1952-1952/android.techbreeze.in W/dalvikvm﹕ threadid=1: thread exiting with   uncaught exception (group=0xb0ec4648) 
12-25 09:28:10.470 1952-1952/android.techbreeze.in E/AndroidRuntime﹕ FATAL EXCEPTION: main 
 java.lang.IllegalStateException: Could not find a method addclick(View) in the activity class   com.example.abhi.myapplication2.login for onClick handler on view class android.widget.Button with id    'addb' 
at android.view.View$1.onClick(View.java:3620) 
at android.view.View.performClick(View.java:4240) 
at android.view.View$PerformClick.run(View.java:17721) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
 Caused by: java.lang.NoSuchMethodException: addclick [class android.view.View] 
at java.lang.Class.getConstructorOrMethod(Class.java:423) 
 at java.lang.Class.getMethod(Class.java:787) 
at android.view.View$1.onClick(View.java:3613) 
        at android.view.View.performClick(View.java:4240) 
        at android.view.View$PerformClick.run(View.java:17721) 
        at android.os.Handler.handleCallback(Handler.java:730) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5103) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:525) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method)

Remove these line, 删除这些行,

style="@android:style/Widget.Material.Button.Borderless"
android:onClick="addclick"

under the Button tag in add.xml add.xml的Button标记add.xml

从“ addb”按钮中删除android:onClick =“ addclick”

I see two ways of solving this problem. 我看到解决此问题的两种方法。 First: initialize your Context variable right in the onCreate method, like this: 首先:在onCreate方法中初始化Context变量,如下所示:

public class SomeActivity extends Activity{
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    addListenerOnButton();
    contex = this;
    }
...
}

Second: do the same, still use not this but the following: 第二:做同样的事情,仍然不使用this但要使用以下方法:

contex = getApplicationContext();

because Intents like usage of application context very much. 因为Intent非常喜欢应用程序上下文的使用。

Try this 尝试这个

 Intent intent = new Intent(Add.this, WifiConnections.class);

and also remove the 并删除

final Context context = this;

Hope it helps ... :) 希望能帮助到你 ... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM