未知异常android

时间:2011-02-14 11:30:29

标签: android

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

它适用于其余按钮(休息活动只包含文本视图和按钮)而在课程视图中,我添加了3个按钮(在我添加这3个按钮之前,它用于在主菜单和课程之间切换)。

我想做的是:

在课程活动中制作3个按钮。他们将引导我进入“高等教育”或“继续教育”等其他观点。在下一个视图中,我将创建一个列表,显示课程名称(如“CCNA”或“健康文凭”!)。当用户点击课程时,它会显示一张图片和一些关于课程的文字信息。

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

        });

    }
}

课程代码视图“Courses.java”在下面给出了,,,,

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

            });
        }   
    }

以及进一步的教育活动的代码如下,,,,

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

            });
        }   
    }

我的Manifest编码如下

<?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> 

2 个答案:

答案 0 :(得分:1)

根据logcat屏幕截图,您的问题是Button next为null,因为它是第63行中的NullPointerException,当您致电next.setOnClickListener时,这意味着R.id.Button01不在R.layout.courses,您能否验证此信息是否正确?

答案 1 :(得分:1)

您将在logcat屏幕中看到应用程序中带有类名的第一行是'Courses.java'。所以,那里出现错误。在logcat中,您将在Courses.java第63行看到NullPointerException。在Courses.java的第63行中,您已为下一个按钮设置了onclick侦听器。所以,我认为当这一行执行时,下一个按钮保持为null。我认为主要错误是第21行

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

我认为布局文件(corses.xml)中下一个按钮的id不是Button01。请修复此事,然后就不会有错误。

试试吧。 :)