我已经待了一个星期,所以我在这里急需帮助。 我已经将zip_file包导入Eclipse中的这个示例项目,但它并不想工作。 obb扩展文件位于测试设备的正确文件夹中。我做错了什么?
日志:
01-04 13:48:17.174: W/System.err(32744): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetFileDescriptor com.android.vending.expansion.zipfile.ZipResourceFile.getAssetFileDescriptor(java.lang.String)' on a null object reference
01-04 13:48:17.177: W/System.err(32744): at com.imre.testzip2.MainActivity$1.onClick(MainActivity.java:97)
01-04 13:48:17.177: W/System.err(32744): at android.view.View.performClick(View.java:4756)
01-04 13:48:17.178: W/System.err(32744): at android.view.View$PerformClick.run(View.java:19749)
01-04 13:48:17.178: W/System.err(32744): at android.os.Handler.handleCallback(Handler.java:739)
01-04 13:48:17.178: W/System.err(32744): at android.os.Handler.dispatchMessage(Handler.java:95)
01-04 13:48:17.178: W/System.err(32744): at android.os.Looper.loop(Looper.java:135)
01-04 13:48:17.178: W/System.err(32744): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-04 13:48:17.178: W/System.err(32744): at java.lang.reflect.Method.invoke(Native Method)
01-04 13:48:17.178: W/System.err(32744): at java.lang.reflect.Method.invoke(Method.java:372)
01-04 13:48:17.178: W/System.err(32744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-04 13:48:17.178: W/System.err(32744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
java代码:
import java.io.IOException;
import com.android.vending.expansion.zipfile.APKExpansionSupport;
import com.android.vending.expansion.zipfile.ZipResourceFile;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
public MediaPlayer wholeTextPlayer;
AssetFileDescriptor descriptor;
ZipResourceFile expansionFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(getApplicationContext(),1,0);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// The button clicks are like follows:
final Button btn_default_acc_hun_whole = (Button) findViewById(R.id.btn_default_acc_hun_whole);
btn_default_acc_hun_whole.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (playerState == PlayerState_DefAcc) {
try {
descriptor = expansionFile.getAssetFileDescriptor("l1g_df_l2.mp3");
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} catch (Exception e) {
e.printStackTrace();
}
} else if (playerState == PlayerState_DefAcc_Paused) {
try {
descriptor = expansionFile.getAssetFileDescriptor("l1g_df_l2.mp3");
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} catch (Exception e) {
e.printStackTrace();
}
} else if (playerState == PlayerState_UsAcc) {
try {
descriptor = expansionFile.getAssetFileDescriptor("l1g_df_l2.mp3");
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} catch (Exception e) {
e.printStackTrace();
}
} else if (playerState == PlayerState_UsAcc_Paused) {
try {
descriptor = expansionFile.getAssetFileDescriptor("l1g_df_l2.mp3");
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} catch (Exception e) {
e.printStackTrace();
}
} else if (playerState == PlayerState_DefAccHun) {
wholeTextPlayer.pause();
setPlayerState(PlayerState_DefAccHun_Paused);
} else if (playerState == PlayerState_DefAccHun_Paused) {
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} else {
try {
descriptor = expansionFile.getAssetFileDescriptor("l1g_df_l2.mp3");
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_DefAccHun);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private final int PlayerState_DefAccHun = 1;
private final int PlayerState_DefAccHun_Paused = 2;
private final int PlayerState_DefAcc = 3;
private final int PlayerState_DefAcc_Paused = 4;
private final int PlayerState_UsAcc = 5;
private final int PlayerState_UsAcc_Paused = 6;
private int playerState;
private void setPlayerState(int state) {
playerState = state;
}
@Override
public void onBackPressed() {
if (wholeTextPlayer != null) {
wholeTextPlayer.release();
}
finish();
super.onBackPressed();
}
}
我只有一个主扩展文件,没有补丁 我是否必须更改zip_file包java类中的任何内容?
答案 0 :(得分:0)
你没有初始化
public MediaPlayer wholeTextPlayer;
在你的oncreate中:
wholeTextPlayer = new MediaPlayer();
当你测试玩家状态时。
playerState = wholeTextPlayer.getPlayerState();
此外:
大部分代码都可以模块化。当您重复相同的代码行时,它表明您可以创建一个方法来管理它。至少你可以创建一个方法来传递每个描述符,并且只是查看下面的代码,还有空间来修剪它。每次单击按钮都可以调用以下方法:
public void playerState(AssetFileDescriptor descriptor){
if (playerState == PlayerState_UsAcc) {
wholeTextPlayer.pause();
setPlayerState(PlayerState_UsAcc_Paused);
} else if (playerState == PlayerState_UsAcc_Paused) {
wholeTextPlayer.start();
setPlayerState(PlayerState_UsAcc);
else{
try {
wholeTextPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
wholeTextPlayer.prepare();
wholeTextPlayer.start();
setPlayerState(PlayerState_UsAcc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果状态之间的代码发生了什么不同,我建议使用switch语句,切换state的值。并且还可以模块化任何重复的代码。