应用程序在导航到不同的活动时崩溃

时间:2014-12-25 13:09:26

标签: android android-activity

我试图让我的应用程序导航到按钮单击的另一个活动,但每当我这样做时,应用程序只是崩溃。我不明白为什么会这样。

我在add.java页面中的代码,该代码将处理按钮点击并导航到其他类WifiConnections

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);

    }

});

}

但是,当我将Wificonnections.xml设置为清单文件中的起始布局时,wifi连接部分工作正常,但在尝试导航时崩溃。

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方法

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...");
    }

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>

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

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)

4 个答案:

答案 0 :(得分:1)

删除这些行,

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

add.xml

中的Button标记下

答案 1 :(得分:1)

从“adb”按钮

删除android:onClick =“添加点击”

答案 2 :(得分:0)

我看到两种解决这个问题的方法。 首先:在onCreate方法中初始化Context变量,如下所示:

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

第二:做同样的事情,仍然不使用this,而是使用以下内容:

contex = getApplicationContext();

因为Intent非常喜欢使用应用程序上下文。

答案 3 :(得分:0)

试试这个

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

并删除

final Context context = this;

希望它有帮助...:)