0

我有一个活动类 MainActivity2.java,它有一个方法 displaySongData(); 其中代码如下:

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

到目前为止一切正常,MainActivity2 正在调用其他活动。我将此代码移至另一个非活动类,并将 if 和 else 语句中的逻辑拆分为不同的方法。对于新类的构造函数,我从 MainActivity2 类中传递了 MainActivity2 实例和 searchSong 对象。但是,当执行包含 if else 的第三部分的方法时,我得到一个空指针异常。

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

songActivity.java 包 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);
}
}

在 Android 中这样做是否违法?有没有办法让我可以在 MainActivity2 以外的单独类中拥有这个逻辑,因为它很混乱?

4

1 回答 1

3

这里

public Bundle bundle = null; <<</// null

您需要先初始化bundle实例,然后再将其用作:

public songDetails(Searchsong search, MainActivity2 context){
    this.searchsong = search;
    this.context = context;
    bundle=new Bundle();   //<<< here initialize bundle
 }
于 2013-03-19T05:25:13.647 回答