在setOnClickListener强制关闭错误(从Android电子打招呼书第3.5例) [英] Force close error on setOnClickListener (Example from hello Android book section 3.5)

查看:228
本文介绍了在setOnClickListener强制关闭错误(从Android电子打招呼书第3.5例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在点击一个按钮,启动一个新的活动的问题,这是我的code:

 包test.project;进口android.app.Activity;
进口android.os.Bundle;
进口android.content.Intent;
进口android.view.View;
进口android.view.View.OnClickListener;公共类测试活动扩展了活动实现OnClickListener {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);  查看aboutButton = findViewById(R.id.about_content);
        aboutButton.setOnClickListener(本);
    }
公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.about_content:
        意图I =新意图(这一点,testit.class);
        startActivity(ⅰ);
        打破;
        //更多的按钮放在这里(如果有的话)...
        }
    }
}

谁能帮我纠正这一错误

错误行

  aboutButton.setOnClickListener(本);

main.xml中的文件

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/你好/>    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=点击这里机器人:layout_gravity =中心
机器人:文字=点击这里机器人:layout_gravity =中心机器人:layout_marginTop =30dip/>
< / LinearLayout中>

包含about_content XML文件是

 <?XML版本=1.0编码=UTF-8&GT?;<滚动型
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=10dip>
    <的TextView
        机器人:ID =@ + ID / about_content
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ about_text/>
< /滚动型>

about_content是已经在这里定义

 <的TextView
            机器人:ID =@ + ID / about_content
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/ about_text/>


解决方案

好了,你不发表您的logcat输出,但由于这是这样一个共同的初学者的错误,我要带胡乱猜测,说你很可能得到一个 NullPointerException异常

您为 findViewById 很可能返回调用,这意味着系统无法找到相关联的视图通过 R.id.about_content 给出的ID。我会仔细检查拼写错误的XML布局。

I am having problem in starting a new activity on clicking a button, here's my code:

package test.project;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class TestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

  View aboutButton = findViewById(R.id.about_content);
        aboutButton.setOnClickListener(this);
    }
public void onClick(View v) {
        switch (v.getId()) {
        case R.id.about_content:
        Intent i = new Intent(this, testit.class);
        startActivity(i);
        break;
        // More buttons go here (if any) ...
        }
    }
}

Can anyone please help me correct this error

Error Line

aboutButton.setOnClickListener(this);

Main.xml file

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here" android:layout_gravity="center"
android:text="Click here" android:layout_gravity="center" android:layout_marginTop="30dip"/>
</LinearLayout>

XML file containing about_content is

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip" >
    <TextView
        android:id="@+id/about_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/about_text" />
</ScrollView>

about_content is already defined here

<TextView
            android:id="@+id/about_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/about_text" />

解决方案

Well, you didn't post your logcat output, but since this is such a common beginner's mistake, I'm going to take a wild guess and say that you are probably getting a NullPointerException.

Your call to findViewById is probably returning null, which means that the system could not find the view associated with the id given by R.id.about_content. I would double check your XML layout for typos.

这篇关于在setOnClickListener强制关闭错误(从Android电子打招呼书第3.5例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆