unknown Exception android
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(), Get开发者_开发知识库tingHere.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. :)
According to the logcat screenshot, your problem is that Button next is null, since it is a NullPointerException
in line 63, which is when you call next.setOnClickListener
, which means that R.id.Button01
is not in R.layout.courses
, could you verify if this information is correct?
精彩评论