我遇到了一种情况,即我的两个活动应该调用相同的功能。 MainActivity和SMSActivity。我编写了一个HelperClass并创建了一个函数getTagSelectionFromDialogBox()。 getTagSelectionFromDialogBox()显示警告对话框并完成一些工作。
我正在从MainActivity实例化HelperClass并通过传递上下文调用helperclass.getTagSelectionFromDialogBox()工作正常。
但是当我从SMSActivity执行相同操作时,应用程序崩溃了。我认为这是因为错误的背景但不确定。
当启动HelperClass()时,我从MainActivity调用HelperClass.getinstance(getApplicationContext()),当从SMSActivity执行时,我调用HelperClass.getinstalce(getApplicationContext())。
以下是MainActivity的代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/shared_array.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <boost/version.hpp>
using namespace boost::interprocess;
struct InData;
typedef boost::interprocess::allocator<InData, managed_shared_memory::segment_manager> salloc;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, salloc::rebind<char>::other> shared_string;
typedef boost::interprocess::vector<float, salloc::rebind<float>::other> shared_float_vec;
struct InData {
int X, Y, H, W;
InData(salloc alloc) : Label(alloc), Floats(alloc) {}
shared_string Label;
shared_float_vec Floats;
};
int main() {
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minor version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
shared_memory_object::remove("MySharedMemory");
managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);
salloc alloc_inst(managed_shm.get_segment_manager());
InData tData(alloc_inst); // = managed_shm.construct<InData>("InDataStructure")();
tData.Label = "Hello World";
tData.Floats.push_back(3.14);
tData.H = 1;
tData.W = 1;
tData.Y = 1;
tData.X = 1;
typedef boost::interprocess::list<InData, salloc> MyList;
MyList *myvector = managed_shm.construct<MyList>("MyVector")(alloc_inst);
myvector->push_back(tData);
}
来自SMSActivity
HelperClass helperclass = HelperClass.getInstalce(getApplicationContext());
tagHandler.getTagSelectionFromDialogBox(rowData);
HelperClass代码的一部分是
HelperClass helperclass = HelperClass.getInstalce(getApplicationContext());
helperclass.getTagSelectionFromDialogBox(rowData);
}
Logcat显示以下错误
public class HelperClass {
Context context = null;
private static HelperClass HelperClass = null;
private HelperClass(Context context) {
this.context = context;
}
public static synchronized HelperClass getInstalce(Context context) {
if(helperclass == null) {
helperclass = new TagHandler(context.getApplicationContext());
}
return helperclass;
}
public void getTagSelectionFromDialogBox(final RowData rowData) {
String selectedTitle = null;
String[] navTitles = = {"one", "tow", "Three"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context.getApplicationContext());
alertDialog.setTitle("Select Category");
alertDialog.setSingleChoiceItems(navTitles, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int title_position = which;
String selectedTitle = navTitles[which];
dialog.cancel();
if (rowData.getInfoPath() != null) {
//doing some work
} else {
//doing some work
}
}
});
AlertDialog ad = alertDialog.create();
ad.show();
}
其中HelperClass.java:75是ad.show()。我怀疑问题可能是通过上下文但不确定如何解决问题。
MainActivity代码部分。它是巨大的,所以把它的onCate()代码:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:570)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:306)
at com.srids.tagit.TagHandler.getTagSelectionFromDialogBox(HelperClass.java:75)
at com.srids.tagit.SMSActivity$2.onClick(SMSActivity.java:140)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
}
}
答案 0 :(得分:1)
您所做的并不是处理此类情况的正确方法更新您的代码
public class HelperClass {
public static void getTagSelectionFromDialogBox(final Context context,final RowData rowData) {
String selectedTitle = null;
String[] navTitles = = {"one", "tow", "Three"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Select Category");
alertDialog.setSingleChoiceItems(navTitles, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int title_position = which;
String selectedTitle = navTitles[which];
dialog.cancel();
if (rowData.getInfoPath() != null) {
//doing some work
} else {
//doing some work
}
}
});
AlertDialog ad = alertDialog.create();
ad.show();
}
}
用法
HelperClass.getTagSelectionFromDialogBox(MainActivty.this,rowData);
用下面的
替换MainActivity代码 public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.FragmentDrawerListener, shareDataInterface {
private Toolbar toolbar;
NavigationDrawerFragment navigationDrawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("tagIt");
toolbar.setSubtitle("make search easy..");
toolbar.setTitleTextColor(getResources().getColor(R.color.colorAccent));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
utils= new Utils();
prefs = MyPrefs.getInstance(this);
if(prefs.getBool(MyGlobals.PASSWORD_PROTECT) == true) {
if(loggedin == 0) {
loggedin = 1;
utils.showDialog(MainActivity.this, MyGlobals.DIALOG_PIN_INPUT);
}
}
// populate navigation drawer
navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
navigationDrawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
navigationDrawerFragment.setDrawerListener(this);
// establish data base connection and first read tags list for NAV drawer
db = DatabaseHelper.getInstance(getApplicationContext());
navTitles =navigationDrawerFragment.titles;
//Get your rowdata before calling the method
HelperClass.getTagSelectionFromDialogBox(MainActivty.this,rowData);
答案 1 :(得分:1)
您不应该使用应用程序上下文来显示对话框而不是活动。没有视图用于附加到应用程序Context的对话框。如果要在MainActivity和SMSActivity上显示两个对话框,则需要更新HelperClass类中的上下文值。
也许你可以改变你的代码:
MainActivity
HelperClass helperclass = HelperClass.getInstalce();
helperclass .getTagSelectionFromDialogBox(MainActivity.this, rowData);
SMSActivity
HelperClass helperclass = HelperClass.getInstalce();
helperclass .getTagSelectionFromDialogBox(SMSActivity.this, rowData);
HelperClass
public class HelperClass {
private static HelperClass helperclass = null;
private HelperClass() {
}
public static synchronized HelperClass getInstalce() {
if (helperclass == null) {
helperclass = new HelperClass();
}
return helperclass;
}
public void getTagSelectionFromDialogBox(Context context, final RowData rowData) {
String selectedTitle = null;
String[] navTitles = {"one", "tow", "Three"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Select Category");
alertDialog.setSingleChoiceItems(navTitles, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int title_position = which;
String selectedTitle = navTitles[which];
dialog.cancel();
if (rowData.getInfoPath() != null) {
//doing some work
} else {
//doing some work
}
}
});
AlertDialog ad = alertDialog.create();
ad.show();
}
}
答案 2 :(得分:0)
除了anoop的答案之外,请执行此操作以获得更好的性能 如果您正在使用应用程序类,请将以下代码放在应用程序类中,
HelperClass mHelperClass;
public final HelperClass getHelperClass() {
if(mHelperClass == null) {
mHelperClass = new HelperClass();
}
return mHelperClass;
}
并在任何地方使用getHelperClass()方法。
希望这可以帮到你..