0

我试图在单击按钮后加载第二个布局,但由于某种原因,应用程序不断崩溃......我很确定我做的一切都是正确的 - 但每次点击后它仍然由于某种原因失败屏幕底部的保存按钮。

附言

如果需要更多信息,请告诉我。我很乐意提供。


JAVA

package com.example.linkingmanager;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;

public class App2Activity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
}
public void saveButtonClicked(View v) {
    // do stuff
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(App2Activity.this, AppActivity.class);
    App2Activity.this.startActivity(intent);

   }

    }

XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Settings:" />
    <TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data Limit" />
    <SeekBar android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10MB" android:layout_weight="1.0" />
        <TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Data" android:layout_weight="1.0" />
    </LinearLayout>
    <TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bandwidth Limit" />
    <SeekBar android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10kbs" android:layout_weight="1.0" />
        <TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Bandwidth" android:layout_weight="1.0" />
    </LinearLayout>
    <TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WiFi Time Limit" />
    <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50.0dip" android:layout_weight="1.0" />
    <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Blocked Sites - [ex: www.xxx.com]" android:ems="10" />
    <Button android:id="@+id/saveButton" android:onClick="saveButtonClicked" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Save"     />
    </LinearLayout>

LOGCAT

03-04 07:35:41.939: D/dalvikvm(944): GC_FOR_ALLOC freed 40K, 7% free 2456K/2640K, paused 46ms, total 49ms
03-04 07:35:41.989: I/dalvikvm-heap(944): Grow heap (frag case) to 5.831MB for 3456016-byte allocation
03-04 07:35:42.049: D/dalvikvm(944): GC_FOR_ALLOC freed 2K, 4% free 5828K/6016K, paused 58ms, total 59ms
03-04 07:35:42.139: D/dalvikvm(944): GC_CONCURRENT freed <1K, 4% free 5828K/6016K, paused 8ms+16ms, total 90ms
03-04 07:35:42.799: D/libEGL(944): loaded /system/lib/egl/libEGL_emulation.so
03-04 07:35:42.878: D/(944): HostConnection::get() New Host Connection established 0x2a155228, tid 944
03-04 07:35:42.959: D/libEGL(944): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-04 07:35:42.979: D/libEGL(944): loaded /system/lib/egl/libGLESv2_emulation.so
03-04 07:35:43.099: W/EGL_emulation(944): eglSurfaceAttrib not implemented
03-04 07:35:43.119: D/OpenGLRenderer(944): Enabling debug mode 0
03-04 07:35:47.248: W/EGL_emulation(944): eglSurfaceAttrib not implemented
03-04 07:35:55.629: D/AndroidRuntime(944): Shutting down VM
03-04 07:35:55.671: W/dalvikvm(944): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-04 07:35:55.709: E/AndroidRuntime(944): FATAL EXCEPTION: main
03-04 07:35:55.709: E/AndroidRuntime(944): java.lang.IllegalStateException: Could not find a method saveButtonClicked(View) in the activity class com.example.linkingmanager.App2Activity for onClick handler on view class android.widget.Button with id 'saveButton'
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.view.View$1.onClick(View.java:3586)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.view.View.performClick(View.java:4204)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.view.View$PerformClick.run(View.java:17355)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.os.Handler.handleCallback(Handler.java:725)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.os.Looper.loop(Looper.java:137)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 07:35:55.709: E/AndroidRuntime(944):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 07:35:55.709: E/AndroidRuntime(944):  at java.lang.reflect.Method.invoke(Method.java:511)
03-04 07:35:55.709: E/AndroidRuntime(944):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 07:35:55.709: E/AndroidRuntime(944):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 07:35:55.709: E/AndroidRuntime(944):  at dalvik.system.NativeStart.main(Native Method)
03-04 07:35:55.709: E/AndroidRuntime(944): Caused by: java.lang.NoSuchMethodException: saveButtonClicked [class android.view.View]
03-04 07:35:55.709: E/AndroidRuntime(944):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-04 07:35:55.709: E/AndroidRuntime(944):  at java.lang.Class.getMethod(Class.java:915)
03-04 07:35:55.709: E/AndroidRuntime(944):  at android.view.View$1.onClick(View.java:3579)
03-04 07:35:55.709: E/AndroidRuntime(944):  ... 11 more
03-04 07:35:58.489: I/Process(944): Sending signal. PID: 944 SIG: 9
4

4 回答 4

0

尝试这个

    public class App2Activity extends Activity {

Button button;

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

    button = (Button) findViewById(R.id.saveButton)
    button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(App2Activity.this, AppActivity.class);
                startActivity(intent);
            }
        });
}
    }

这样做会容易得多。

于 2013-03-04T08:32:40.597 回答
0

您似乎只显示了 xml 的一部分。带有单击侦听器声明的按钮在哪里?我的猜测 - xml 和代码中的方法名称不同。

顺便说一句,不要在除 onCreate() 之外的任何地方调用 super.onCreate(Bundle)。

于 2013-03-04T07:46:30.650 回答
0

另一种方法是在您的活动中声明您的按钮,然后对其调用 onClickListner。

    button = (Button)findViewById(R.id.saveButton);
    button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(App2Activity.this,AppActivity.class);
                    App2Activity.this.startActivity(intent);
                }
    });

android:onClick="saveButtonClicked"从 xml中删除

否则请使用 Prateek Sharma 给出的解决方案

于 2013-03-04T07:47:48.590 回答
0

删除此行:

super.onCreate(savedInstanceState);

从你的方法:

public void saveButtonClicked(View v)

谢谢

于 2013-03-04T07:43:22.453 回答