未知异常机器人 [英] unknown Exception android

查看:151
本文介绍了未知异常机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主文件,它具有图像按钮,它给了我一个例外。当我点击课程图像按钮,它只是关闭应用程序。

它适用于按键的休息(休息活动只是由一个文本视图和按钮),而在课程查看,我添加了3个按钮(之前我加这​​3个按钮,它用来为主要和课程之间的切换工作)。

我所试图做的是这样的:

请在课程活动3个按钮。他们将导航我喜欢高等教育或继续教育等看法。在明年的意见,我会创建一个显示课程名称列表(如CCNA或健康文凭!)。当用户点击过程中,它会显示一个画面和有关课程的一些文字资料。

 包com.NVT.android;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ImageButton;公共类主要活动延伸
{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的ImageButton CoursesButton =(的ImageButton)findViewById(R.id.C​​oursesButton);
        的ImageButton ILoveNescotButton =(的ImageButton)findViewById(R.id.ILoveNescotButton);
        的ImageButton CampusMapButton =(的ImageButton)findViewById(R.id.C​​ampusMapButton);
        的ImageButton GettingHereButton =(的ImageButton)findViewById(R.id.GettingHereButton);        CoursesButton.setOnClickListener(新ImageButton.OnClickListener()
        {            @覆盖
            公共无效的onClick(查看视图)
            {
                            意图myIntent =新意图(view.getContext(),Courses.class);
                            startActivityForResult(myIntent,0);
            }        });        ILoveNescotButton.setOnClickListener(新ImageButton.OnClickListener()
        {            @覆盖
            公共无效的onClick(查看视图)
            {
                            意图myIntent =新意图(view.getContext(),ILoveNescot.class);
                            startActivityForResult(myIntent,0);
            }        });        CampusMapButton.setOnClickListener(新ImageButton.OnClickListener()
        {            @覆盖
            公共无效的onClick(查看视图)
            {
                            意图myIntent =新意图(view.getContext(),CampusMap.class);
                            startActivityForResult(myIntent,0);
            }        });        GettingHereButton.setOnClickListener(新ImageButton.OnClickListener()
        {            @覆盖
            公共无效的onClick(查看视图)
            {
                            意图myIntent =新意图(view.getContext(),GettingHere.class);
                            startActivityForResult(myIntent,0);
            }        });    }
}

code的课程观Courses.java下面给出了,,,,

 包com.NVT.android;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;    公共类课程扩展活动
    {        / **当第一次创建活动调用。 * /
        公共无效的onCreate(捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.courses);
            按钮ButtonFurtherEducation =(按钮)findViewById(R.id.Button_Courses_FurtherEducation);
            按钮ButtonHigherEducation =(按钮)findViewById(R.id.Button_Courses_HigherEducation);
            按钮ButtonEmployernTraining =(按钮)findViewById(R.id.Button_Courses_EmployersnTraining);
            按钮旁边=(按钮)findViewById(R.id.Button01);
            ButtonFurtherEducation.setOnClickListener(新Button.OnClickListener()
            {                @覆盖
                公共无效的onClick(查看视图)
                {
                                意图myIntent =新意图(view.getContext(),FurtherEducationCourses.class);
                                startActivityForResult(myIntent,0);
                }            });
            ButtonHigherEducation.setOnClickListener(新Button.OnClickListener()
            {                @覆盖
                公共无效的onClick(查看视图)
                {
                                意图myIntent =新意图(view.getContext(),HigherEducationCourses.class);
                                startActivityForResult(myIntent,0);
                }            });            ButtonEmployernTraining.setOnClickListener(新Button.OnClickListener()
            {                @覆盖
                公共无效的onClick(查看视图)
                {
                                意图myIntent =新意图(view.getContext(),EmployersTrainingCourses.class);
                                startActivityForResult(myIntent,0);
                }            });            next.setOnClickListener(新View.OnClickListener()
            {
                公共无效的onClick(查看视图)
                {
                    意向意图=新的Intent();
                    的setResult(RESULT_OK,意向);
                    完();
                }            });
        }
    }

和code,为进一步教育活动,,,,下面给出

 包com.NVT.android;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;    公共类FurtherEducationCourses扩展活动
    {        / **当第一次创建活动调用。 * /
        公共无效的onCreate(捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.further_education);            按钮旁边=(按钮)findViewById(R.id.Button01);
            next.setOnClickListener(新View.OnClickListener()
            {
                公共无效的onClick(查看视图)
                {
                    意向意图=新的Intent();
                    的setResult(RESULT_OK,意向);
                    完();
                }            });
        }
    }

我的清单编码如下

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.NVT.android
      安卓版code =1
      机器人:=的versionName1.0>
    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>        <活动机器人:主NAME =
                  机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动机器人:课程NAME =>
        < /活性GT;
        <活动机器人:CampusMapNAME =>
        < /活性GT;
        <活动机器人:名字=。GettingHere>
        < /活性GT;
        <活动机器人:名字=。ILoveNescot>
        < /活性GT;
        <活动机器人:FurtherEducationCoursesNAME =>
        < /活性GT;
        <活动机器人:HigherEducationCoursesNAME =>
        < /活性GT;
        <活动机器人:EmployersTrainingCoursesNAME =>
        < /活性GT;    < /用途>
    <采用-SDK安卓的minSdkVersion =9/>< /清单>


解决方案

您将在logcat的屏幕上看到,与类名的第一行从您的应用程序Courses.java。这样,发生的错误。在logcat中,你会看到在的NullPointerException行Courses.java 63. Courses.java你已经设置的onclick监听下一个按钮行63。所以,我认为下一步按钮留空该行执行时。我认为主要的错误是在无行21

 按钮旁边=(按钮)findViewById(R.id.Button01);

我觉得在布局文件(corses.xml)下一个按钮的id是不是Button01。请修正这个东西,然后就会出现任何错误。

试试吧。 :)

This is my main file, which has image buttons, and it gives me an exception. When I click on courses image button, it just closes the application.

It works for rest of buttons (rest activities just consist a text view and button) whereas in courses view, I added 3 more buttons (before I added these 3 buttons, it used to work for switching between main and courses).

What i am trying to do is this:

Make 3 buttons in courses activity. They will navigate me to other views like "higher education" or "further education". In next views I will create a list which will display name of courses (like "CCNA" or "Health diploma"!). When the user clicks on course, it will display a picture and some text information about the course.

package com.NVT.android;

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

public class Main extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageButton CoursesButton = (ImageButton)findViewById(R.id.CoursesButton);
        ImageButton ILoveNescotButton = (ImageButton)findViewById(R.id.ILoveNescotButton);
        ImageButton CampusMapButton = (ImageButton)findViewById(R.id.CampusMapButton);
        ImageButton GettingHereButton = (ImageButton)findViewById(R.id.GettingHereButton);

        CoursesButton.setOnClickListener(new ImageButton.OnClickListener()
        {

            @Override
            public void onClick(View view) 
            {
                            Intent myIntent = new Intent(view.getContext(), Courses.class);
                            startActivityForResult(myIntent, 0);
            }

        });

        ILoveNescotButton.setOnClickListener(new ImageButton.OnClickListener()
        {

            @Override
            public void onClick(View view) 
            {
                            Intent myIntent = new Intent(view.getContext(), ILoveNescot.class);
                            startActivityForResult(myIntent, 0);
            }

        });

        CampusMapButton.setOnClickListener(new ImageButton.OnClickListener()
        {

            @Override
            public void onClick(View view) 
            {
                            Intent myIntent = new Intent(view.getContext(), CampusMap.class);
                            startActivityForResult(myIntent, 0);
            }

        });

        GettingHereButton.setOnClickListener(new ImageButton.OnClickListener()
        {

            @Override
            public void onClick(View view) 
            {
                            Intent myIntent = new Intent(view.getContext(), GettingHere.class);
                            startActivityForResult(myIntent, 0);
            }

        });

    }
}

Code for Courses View "Courses.java" is given below,,,,

package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

    public class Courses extends Activity 
    {

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.courses);


            Button ButtonFurtherEducation = (Button)findViewById(R.id.Button_Courses_FurtherEducation);
            Button ButtonHigherEducation = (Button)findViewById(R.id.Button_Courses_HigherEducation);
            Button ButtonEmployernTraining = (Button)findViewById(R.id.Button_Courses_EmployersnTraining);
            Button next = (Button) findViewById(R.id.Button01);


            ButtonFurtherEducation.setOnClickListener(new Button.OnClickListener()
            {

                @Override
                public void onClick(View view) 
                {
                                Intent myIntent = new Intent(view.getContext(), FurtherEducationCourses.class);
                                startActivityForResult(myIntent, 0);
                }

            });


            ButtonHigherEducation.setOnClickListener(new Button.OnClickListener()
            {

                @Override
                public void onClick(View view) 
                {
                                Intent myIntent = new Intent(view.getContext(), HigherEducationCourses.class);
                                startActivityForResult(myIntent, 0);
                }

            });

            ButtonEmployernTraining.setOnClickListener(new Button.OnClickListener()
            {

                @Override
                public void onClick(View view) 
                {
                                Intent myIntent = new Intent(view.getContext(), EmployersTrainingCourses.class);
                                startActivityForResult(myIntent, 0);
                }

            });



            next.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View view) 
                {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }

            });
        }   
    }

and code for the further Education Activity is given below,,,,

package com.NVT.android;

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

    public class FurtherEducationCourses extends Activity 
    {

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.further_education);

            Button next = (Button) findViewById(R.id.Button01);
            next.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View view) 
                {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }

            });
        }   
    }

my Manifest coding as below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.NVT.android"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".Main"
                  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:name=".Courses">
        </activity>
        <activity android:name=".CampusMap">
        </activity>
        <activity android:name=".GettingHere">
        </activity>
        <activity android:name=".ILoveNescot">
        </activity>


        <activity android:name=".FurtherEducationCourses">
        </activity>
        <activity android:name=".HigherEducationCourses">
        </activity>
        <activity android:name=".EmployersTrainingCourses">
        </activity>







    </application>
    <uses-sdk android:minSdkVersion="9" />

</manifest> 

解决方案

You will see in the logcat screen that the first line with class name from your application is 'Courses.java'. So, the error occurs there. In the logcat you will see NullPointerException at Courses.java line 63. In line 63 of Courses.java you have set the onclick listener for the next button. So, I think the next button stay null when this line executes. I think the main error is in line no 21

Button next = (Button) findViewById(R.id.Button01);

I think the id of the next button in the layout file (corses.xml) is not Button01. Please fix this thing then there will be no errors.

Just try it. :)

这篇关于未知异常机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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