不知道为什么,没有人试图解决这个问题。
在这里,我完全搞砸了。我正在创建一个Android应用程序,目前在3个活动的操作中遇到问题。
我有活动1 ,活动2 和活动3 。
Activity1 :在AsyncTask(background)
中获取数据并显示listView。在listView项目上显示带有onClick
操作的alertDialog。
我在AsyncTask
public NameAsyncTask(Context cxt) {
context = cxt;
pDialog = new ProgressDialog(context);
adapter = new SimpleAdapter(
context, dataList,
R.layout.list_incident, new String[]{"refName", "sub", "status", "categ", "createTime", "organisation"},
new int[]{R.id.in_name, R.id.sub, R.id.status,R.id.categ_show, R.id.created_time_show, R.id.cust_name});
binder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.equals((TextView)view.findViewById(R.id.status))) {
statusText = (TextView)view.findViewById(R.id.status);
if (data.equals("Unresolved"))
statusText.setTextColor(context.getResources().getColor(R.color.red));
else if (data.equals("In Progress"))
statusText.setTextColor(context.getResources().getColor(R.color.yellow));
}
return false;
}
};
}
我将AsyncTask
课程称为new NameAsyncTask(Activity1.this).execute();
Activity2 :显示listView项目的详细信息。最后调用finish()
进入Activity3
。
活动3 :更新该项目的详细信息,并将AsyncTask(background)
的{{1}}作为1st activity
进行调用,以便该项目将在new NameAsyncTask(Activity3.this).execute();
中删除listView将会更新。调用first activity
并直接转到第1个活动(因为第2个活动已经完成)。
现在问题是,如果再次点击listView项目,它会抛出错误
finish()
在这一行
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:149)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at com.lavazza.IncidentServiceActivity$GetPendingIncidents$2.onItemClick(IncidentServiceActivity.java:267)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4048)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3807)
at android.view.View.dispatchTouchEvent(View.java:10023)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2626)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
不确定final AlertDialog.Builder alertDialog = new AlertDialog.Builder(Activity1.this);
是否也被破坏。
因为listView的Activity1
和adapter
适用于两种活动的上下文。但在binder
它不起作用。当我在构造函数中声明alertDialog
时。它在 Activity1 中工作正常。
如果我使用final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
从 Activity3 执行AsyncTask
,则会抛出错误
Activity1Obj.new GetPendingIncidents(Activity3.this).execute();
这里 Activity1 会自动销毁,我相信。如果我错了,请纠正我。
我已经尝试了
- &gt;将alertDialog声明中的android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@1a8ec93 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:322)
at com.lavazza.IncidentServiceActivity$GetPendingIncidents$2.onItemClick(IncidentServiceActivity.java:338)
更改为context
,getParent
,getApplicationContext
。没有任何效果。
从getBaseContext
返回后,我要显示alertDialog
。
有没有人帮助我。我还在考虑如何执行此操作。
答案 0 :(得分:0)
您可以使用MyApplication.getContext()
代替上下文(等等Activity1.this
)。
<强>的AndroidManifest.xml 强>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<强> MyApplication.java 强>
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String app_name = MyApplication.getContext().getResources().getString(R.string.app_name);
Log.d("App name", app_name);
}
}
答案 1 :(得分:0)
您只需要完成显示alertdialog的activity3,无需完成activity1以及2。
并且也使用
context.finish();
其中context = your_activity.this;
希望它会对你有所帮助。