我在MainActivity中创建了一个公共void方法来切换到另一个布局,但每当我调用此方法时,应用程序崩溃。
logcat的:
Status _status = new Status {TotalRecs = query.Count};
var progress = new Progress<Status>(msg => WriteStatusUpdate(msg));
Task.Run(() =>
{
OrderablePartitioner<PayrollHeader> partitioner = Partitioner.Create(query, EnumerablePartitionerOptions.NoBuffering);
Parallel.ForEach(partitioner, thisCheck =>
{
lock (_status)
{
_status.ProcessMsg = "Voucher " + thisCheck.VoucherNumber;
_status.ProcessName = thisCheck.EmployeeName;
_status.CurrentRec = ++iChckNo;
dtSpan = DateTime.Now.Subtract(dtSpanStart);
_status.TimeMsg = string.Format("Elapsed {0}:{1}:{2}", dtSpan.Hours, dtSpan.Minutes, dtSpan.Seconds);
}
((IProgress<Status>) progress).Report(_status);
thisCheck.GetDetails();
});
}).Wait();
private void WriteStatusUpdate(Status _status)
{
lblVoucher.Text = _status.ProcessMsg;
lblVoucher.Refresh();
lblName.Text = _status.ProcessName;
lblName.Refresh();
lblCount.Text = string.Format("Records {0} of {1}", _status.CurrentRec, _status.TotalRecs);
lblCount.Refresh();
lblTime.Text = _status.TimeMsg;
lblTime.Refresh();
}
MainActivity:
07-05 16:35:22.823 14683-14712/com.example.name.mygame I/OpenGLRenderer: Initialized EGL, version 1.4
[ 07-05 16:35:24.724 14683:14754 D/]
HostConnection::get() New Host Connection established 0xae4b2240, tid 14754
07-05 16:35:28.500 14683-14754/com.example.name.mygame I/System.out: gameOverUi called!
-------- beginning of crash
07-05 16:35:28.558 14683-14754/com.example.name.mygame E/AndroidRuntime: FATAL EXCEPTION: Thread-218
Process: com.example.name.mygame, PID: 14683
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Window.setContentView(int)' on a null object reference
at android.app.Activity.setContentView(Activity.java:2166)
at com.example.name.mygame.MainActivity.gameOverUi(MainActivity.java:15)
at com.example.name.mygame.GameView.gameOver(GameView.java:16)
at com.example.name.mygame.GameView.onDraw(GameView.java:188)
at com.example.name.mygame.GameLoopThread.run(GameLoopThread.java:37)
07-05 16:35:30.070 14683-14754/com.example.name.mygame I/Process: Sending signal. PID: 14683 SIG: 9
GameView类:
package com.example.name.mygame;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
public void gameOverUi(){
System.out.println("gameOverUi called!");
setContentView(R.layout.gameover); //Crash here
}
}
如何在不导致应用程序崩溃的情况下更改内容视图?任何答案都将不胜感激,谢谢。
答案 0 :(得分:2)
MainActivity mActivity = new MainActivity();
这是你的问题。
这样您就可以创建一个MainActivity的新分配,但是您不能引用您的MainActivity。 为此,您有一个零点异常。
避免这种情况。
答案 1 :(得分:2)
您在MainActivity
内没有正确地引用您的GameView
课程,因为您当前正在创建新对象而没有获得对Activity
的引用正在展示。这不是访问活动的正确方法。
由于Activity
继承自Context
,理论上你可以这样做:
public class GameView extends SurfaceView {
private MainActivity mActivity;
//Create new constructor to get the Activity
public GameView(Activity activity) {
super(activity);
mActivity = (MainActivity) activity;
}
private void gameOver(){
mActivity.gameOverUi();
}
}