如果子类具有不同的上下文并且在单击侦听器之后启动的不同活动被调用,那么超类中的代码Intent intent=new Intent(context,Activity.class)
如何可以被其子类重用。可能吗?
这是超类:
public class CommonPost extends AppCompatActivity {
public void on_create(final Context context, final Class aclass) {
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CommonPost commonPost = new CommonPost();
MyTask task = commonPost.new MyTask(context, aclass);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
task.execute();
}
});
}
class MyTask extends AsyncTask<Void,Void,Void> {
Context context;
Class aclass;
public MyTask(Context context,Class aclass){
this.context=context;
this.aclass=aclass;
}
@Override
protected void onPreExecute() {
// do something
}
@Override
protected Void doInBackground(Void... voids) {
// do something
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(context, aclass);
startActivity(intent);
}
}
其中一个子类:
public class PlacementPost extends CommonPost {
Context context=PlacementPost.this;
Class aclass=Placements.class;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
on_create(context,aclass);
}
}
我尝试上面的代码时收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:4266)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4548)
at android.app.Activity.startActivity(Activity.java:4516)
at studentapp.notefi.CommonPost$PlaceTask.onPostExecute(CommonPost.java:240)
at studentapp.notefi.CommonPost$PlaceTask.onPostExecute(CommonPost.java:177)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我不确定我错在哪里或我错过了什么。我只是尝试了逻辑上我认为正确的东西。请纠正我错在哪里!
答案 0 :(得分:1)
对于初学者,你永远不应该使用new来初始化你的Activity,它有自己的生命周期,上下文应该是你实际开始意图的类,把你的代码改为
<强> on_create 强>
public void on_create(final Context context, final Class aclass) {
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyTask task = new MyTask(stor_root, mProgress, editTextplace, post, ninfo, imageUri,
mstorage, mDatabase, context, aclass);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
task.execute();
}
});
}
MyTask - 停止传递来自孩子的context
class MyTask extends AsyncTask<Void,Void,Void> {
Class aclass;
public MyTask(Class aclass){
this.aclass=aclass;
}
@Override
protected void onPreExecute() {
// do something
}
@Override
protected Void doInBackground(Void... voids) {
// do something
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(CommonPost.this, aclass);
startActivity(intent);
}
}
<强>子类强>
public class PlacementPost extends CommonPost {
Context context=PlacementPost.this;
Class aclass=Placements.class;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
on_create(aclass);
}
}
答案 1 :(得分:0)
您需要将变量设为类范围而不是内部类范围。
这是一个内部类范围变量:
public class CommonPost extends AppCompatActivity {
...
class MyTask extends AsyncTask<Void,Void,Void> {
Context context;
Class aclass;
...
}
}
您无法从子类中访问Context context
和Class aclass;
。
你需要把它变成一个类范围:
public class CommonPost extends AppCompatActivity {
// set to protected to only allow child class access.
protected Context context;
protected Class aclass;
...
class MyTask extends AsyncTask<Void,Void,Void> {
}
}
然后,在您的子类中,将变量更改为:
public class PlacementPost extends CommonPost {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the value to the base class.
context = PlacementPost.this;
aclass = Placements.class;
on_create(context,aclass);
}
}