I'm developing a simple N-Puzzle Android application to become more comfortable with the Android environment (I'm a beginner Android Developer). The issue I've ran into is that I can't seem to get the grid I've created to display properly. I'm at my wits end with this issue, so any help is appreciated. I can post more of the code if needed. When I attempt to run my app, I get a NullPointerException caused by the line I've commented below. I'm not sure what's causing the exception, so any insight is much appreciated.
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
public abstract class Activity extends android.app.Activity{
private Object contentView;
private boolean finished;
private int frameViewCount;
public Activity()
{
}
public Activity(View view)
{
contentView = view;
}
public Activity(int viewResource)
{
contentView = viewResource;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Application app = getApplicationContext();
app.onActivityCreate(this);
finished = false;
super.setContentView(R.layout.frame);
ViewGroup frame = (ViewGroup)findViewById(R.id.viewgroup_app_frame);
frameViewCount = frame.getChildCount();
forceSetContentView((Integer)contentView); //this is where my issue is
}
@Override
public void onDestroy()
{
super.onDestroy();
getApplicationContext().onActivityDestroy(this);
}
@Override
public void setContentView(int resID)
{
if(contentView == null || !contentView.equals(resID))
{
forceSetContentView(resID);
}
}
@Override
public void setContentView(View view, LayoutParams params)
{
ViewGroup frame = (ViewGroup)findViewById(R.id.viewgroup_app_frame);
if (frame.getChildAt(frameViewCount) != null)
frame.removeViewAt(frameViewCount);
addContentToFrame(frame, view, params);
}
@Override
public void setContentView(View view)
{
if (view != contentView)
forceSetContentView(view);
}
@Override
public void onPause()
{
super.onPause();
try{
Editor settings = getSharedPreferences("Activity.prefs", MODE_PRIVATE).edit();
if(finished)
settings.remove("LastActivity");
else
settings.putString("LastActivity", this.getClass().getName());
settings.commit();
}catch(Exception e)
{
Log.w("Activity.java", "Failed to update preferences");
}
}
@Override
public Application getApplicationContext()
{
return (Application)super.getApplication();
}
public void forceSetContentView(int resID)
{
ViewGroup frame = (ViewGroup)findViewById(R.id.viewgroup_app_frame);
if(frame.getChildAt(frameViewCount) != null)
{
frame.removeViewAt(frameViewCount);
}
View view = View.inflate(this, resID, null);
addContentToFrame(frame, view, null);
}
private void forceSetContentView(View view)
{
ViewGroup frame = (ViewGroup)findViewById(R.id.viewgroup_app_frame);
if (frame.getChildAt(frameViewCount) != null)
frame.removeViewAt(frameViewCount);
if (null != view)
addContentToFrame(frame, view, null);
}
public void addContentToFrame(ViewGroup frame, View view, LayoutParams params)
{
RelativeLayout.LayoutParams frameParams;
if (null == params)
frameParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
else if (params instanceof RelativeLayout.LayoutParams)
frameParams = (RelativeLayout.LayoutParams)params;
else
frameParams = new RelativeLayout.LayoutParams(params);
frame.addView(view, frameViewCount, frameParams);
}
}
0 comments:
Post a Comment