68943

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(), 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>

Answer1:

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. :)

Answer2:

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?

Recommend

  • File upload control not working inside update panel
  • Asp controls Id generation inside repeater
  • Android: adding components to RelativeLayout
  • Error inflating class ImageButton
  • How to change placeholder text in an autocomplete activity of android google place?
  • EditText is covered by Keyboard
  • URLConnection doesn't work since API 10 and higher?
  • how does System.Web.HttpRequest::PathInfo work?
  • How to disable all widgets inside Panel or inside Composite?
  • Not able to aggregate on nested fields in elasticsearch
  • how to avoid repetitive constructor in children
  • GridView breaks while scrolling
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Google Custom Search with transparent background
  • Highlight and Bold text in JTextPane
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • Suggestions to manage Login/Logout transitions
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?