I have an activity class MainActivity2.java which had a method displaySongData(); which had code as follows:
if(searchsong.serverErrors.size() > 0)
Toast.makeText(this.context, searchsong.serverErrors.get(0) ,
Toast.LENGTH_LONG).show();
else if(searchsong.errorMsg != "" || searchsong.result == null)
{
intent = new Intent(context,ErrorDisplayActivity.class);
bundle.putString("error",searchsong.errorMsg == "" ? "No such songs could be found." : searchsong.errorMsg);
intent.putExtras(bundle);
context.startActivity(intent);
}
else ////// 3 rd part of if else ***********
{
intent = new Intent(this,DisplaysongDetails.class);
song result = searchsong.result;
bundle.putString("tune", result.tune);
bundle.putString("lyrics", result.lyrics);
bundle.putString("meaning", result.meaning);
intent.putExtras(bundle);
this.startActivity(intent);
}
Everything was working fine upto this point and MainActivity2 was calling other activities. I moved this code to another non-activity class and split up the logic within if and else statements to different methods. For the constructor of the new class, I passed the MainActivity2 instance and the searchSong object from the MainActivity2 class . However when the method containing the third part of the if else is executed, I get a null pointer exception.
03-18 04:21:44.939: E/AndroidRuntime(1163): FATAL EXCEPTION: main
03-18 04:21:44.939: E/AndroidRuntime(1163): java.lang.IllegalStateException: Could not execute method of the activity
03-18 04:21:44.939: E/AndroidRuntime(1163): at android.view.View$1.onClick(View.java:3591)
03-18 04:21:44.939: E/AndroidRuntime(1163): at android.view.View.performClick(View.java:4084)
03-18 04:21:44.939: E/AndroidRuntime(1163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-18 04:21:44.939: E/AndroidRuntime(1163): at dalvik.system.NativeStart.main(Native Method)
03-18 04:21:44.939: E/AndroidRuntime(1163): Caused by: java.lang.reflect.InvocationTargetException
03-18 04:21:44.939: E/AndroidRuntime(1163): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 04:21:44.939: E/AndroidRuntime(1163): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 04:21:44.939: E/AndroidRuntime(1163): at android.view.View$1.onClick(View.java:3586)
03-18 04:21:44.939: E/AndroidRuntime(1163): ... 11 more
03-18 04:21:44.939: E/AndroidRuntime(1163): Caused by: java.lang.NullPointerException
03-18 04:21:44.939: E/AndroidRuntime(1163): at com.song.display.songDetails.navigateToDisplayActivity(songDetails.java:53)
03-18 04:21:44.939: E/AndroidRuntime(1163): at com.song.display.songDetails.processErrorsOrDisplay(songDetails.java:29)
03-18 04:21:44.939: E/AndroidRuntime(1163): at com.song.MainActivity2.displaysongData(MainActivity2.java:221)
03-18 04:21:44.939: E/AndroidRuntime(1163): at com.song.MainActivity2.onClick(MainActivity2.java:170)
03-18 04:21:44.939: E/AndroidRuntime(1163): ... 14 more
Code for songActivity.java package com.songs.display;
import com.songs.MainActivity2;
import com.songs.model.song;
import com.songs.search.Searchsong;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class songDetails implements IDisplayResults {
public Context context;
public Searchsong searchsong;
public Bundle bundle = null;
public Intent intent = null;
public songDetails(Searchsong search, MainActivity2 context){
this.searchsong = search;
this.context = context;
}
public SongDetails(Searchsong search, MainActivity2 context){
this.searchSong = search;
this.context = context;
}
public void processErrorsOrDisplay()
{
if(searchSong.serverErrors.size() > 0) processServerErrors();
else if(searchsong.errorMsg != "" || searchsong.result == null) processResultErrors();
else navigateToDisplayActivity(); /////////LINE 29
}
public void processServerErrors() {
if(searchsong.serverErrors.size() > 0)
{
System.out.println("Number of errors is" + searchsong.serverErrors.size());
Toast.makeText(this.context, searchsong.serverErrors.get(0) ,
Toast.LENGTH_LONG).show();
}
}
public void processResultErrors() {
System.out.println("the error message is NOTT empty here");
intent = new Intent(context,ErrorDisplayActivity.class);
bundle.putString("error",searchsong.errorMsg == "" ? "No such songs could be found." : searchsong.errorMsg);
intent.putExtras(bundle);
context.startActivity(intent);
}
public void navigateToDisplayActivity() {
intent = new Intent(context,DisplaysongDetails.class);
song result = searchsong.result;
System.out.println("tune in method is " + result.tune);
bundle.putString("tune", result.tune); //////////*********** LINE 53
bundle.putString("lyrics", result.lyrics);
bundle.putString("meaning", result.meaning);
intent.putExtras(bundle);
context.startActivity(intent);
}
}
Is it illegal to do this in Android? Is there a way where I can have this logic in a separate class other than MainActivity2 as it is all cluttered?
Here
public Bundle bundle = null; <<</// null
you will need to initialize bundle
instance before using it as :
public songDetails(Searchsong search, MainActivity2 context){
this.searchsong = search;
this.context = context;
bundle=new Bundle(); //<<< here initialize bundle
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.