1

我是新来的和安卓的。我查看了类似的主题,但仍然没有找到解决方案。

这是我的 xml 代码。我想要做的是,在单击提交按钮时,没有单击复选框,然后产生错误或成功消息。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="Enter Your Name" >

    <requestFocus />
  </EditText>



<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox2"
    android:layout_marginLeft="15dp"
    android:text="Hello" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/checkBox3"
    android:layout_below="@+id/checkBox3"
    android:text="Noon" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox3"
    android:layout_below="@+id/checkBox1"
    android:text="Evening" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup2"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:checked="true"
    android:text="Morning" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:onClick="onClickSubmit"
    android:text="Hello" />

这是Java代码...

CheckBox c1=(CheckBox) findViewById(R.id.checkBox1);
Button sub=(Button) findViewById(R.id.submit);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



sub.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v) {  

                    Toast.makeText(MainActivity.this, "Submit!", Toast.LENGTH_SHORT).show();  
            }  
});  

错误日志...

 01-19 14:13:47.102: E/AndroidRuntime(2554): FATAL EXCEPTION: main
   01-19 14:13:47.102: E/AndroidRuntime(2554): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.finalfortoday/com.example.finalfortoday.MainActivity}: java.lang.NullPointerException
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.os.Looper.loop(Looper.java:137)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.reflect.Method.invoke(Method.java:511)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at dalvik.system.NativeStart.main(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554): Caused by: java.lang.NullPointerException
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.Activity.findViewById(Activity.java:1839)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.example.finalfortoday.MainActivity.<init>(MainActivity.java:16)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.Class.newInstanceImpl(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.Class.newInstance(Class.java:1319)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-19 14:13:47.102: E/AndroidRuntime(2554):     ... 11 more

我还没有在 Java 代码中使用过任何东西,但它仍然给我错误。

请帮助...

4

2 回答 2

0

您不能在方法之外进行分配。把它放在 onCreate 里面:

CheckBox c1  = (CheckBox) findViewById(R.id.checkBox1);
Button sub = (Button) findViewById(R.id.submit);

它写在堆栈跟踪中:sub当您尝试为其分配点击侦听器时为空。

此外,Glenn 是对的:android:onClick如果您要手动设置点击侦听器,请删除该属性。


已编辑。我以前的答案:

按钮对齐错误:

android:layout_below="@+id/checkBox4"

checkBox4不存在。

于 2013-01-19T13:44:26.340 回答
0

Better to remove the android:onClick attribute.

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:onClick="onClickSubmit"
    android:text="Hello" />

It should be

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:text="Hello" />
于 2013-01-19T13:48:52.397 回答