我正在尝试在我的应用程序中的各种活动中共享两个ArrayLists,使用此处解释的方案:如何在Android中声明全局变量?.
这是我的应用程序子类:
public class GlobalVars extends Application{
ArrayList<Player> players = new ArrayList<Player>();
ArrayList<String> playerNames = new ArrayList<String>();
public ArrayList<Player> getPlayers(){
return players;
}
public ArrayList<String> getPlayerNames(){
return playerNames;
}
public void setPlayers(ArrayList<Player> p){
players = p;
}
public void setPlayerNames(ArrayList<String> pn){
playerNames = pn;
}
}
Run Code Online (Sandbox Code Playgroud)
并使用了代码:
GlobalVars gv = (GlobalVars)getApplicationContext();
players = gv.getPlayers();
playerNames = gv.getPlayerNames();
Run Code Online (Sandbox Code Playgroud)
要访问这些变量.我定义gv的第一行引发了classcastexception.谁知道为什么?
这是我添加到清单中的代码:
<application android:name="com.myname.GlobalVars"
android:icon="@drawable/icon"
android:label="@string/app_name"></application>
Run Code Online (Sandbox Code Playgroud)
编辑:为了澄清,这是我的整个清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.bpstattracker" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BPStatTracker" android:label="@string/app_name"> …Run Code Online (Sandbox Code Playgroud) 我正在尝试"动画"WebView以下拉并显示其内容.我已经编写了一个处理程序,每次将高度增加1,但是,我遇到了ClassCastException.我正在使用的代码是:
WebView.LayoutParams params = new WebView.LayoutParams(wv.getLayoutParams());
params.height = height;
wv.setLayoutParams(params);
height++;
this.sleep(20);
Run Code Online (Sandbox Code Playgroud)
在wv.setLayoutParams(params)行上,我得到一个:
java.lang.ClassCastException:android.widget.AbsoluteLayout $ LayoutParams
我该如何解决?
这是XML(只是一个webview):
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/couponView" android:layout_height="100dp" android:layout_width="100dp" />
Run Code Online (Sandbox Code Playgroud)
和代码:
final View cView = getLayoutInflater().inflate(R.layout.couponlayout, null);
PopupWindow pw = new PopupWindow(cView);
pw.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 100, 100);
pw.update();
Run Code Online (Sandbox Code Playgroud)
这是在button.onClick()方法中.当我单击按钮时,应该发生的其他事情(按钮改变颜色,文本等),但PopupWindow不会显示.我一直在梳理网络,但找不到任何修复.我究竟做错了什么?
编辑:没人知道发生了什么?我觉得这是一个常见的问题.
我有一个带注释的地图视图,这些注释显示一个标注.单击标注的显示详细信息按钮时,它将转换为新视图.
我的MKAnnotations是一个实现的自定义类<MKAnnotation>.我们称之为MyClass类.它们存储在NSMutableArray中.在此视图的viewdidload期间,我将此数组中MyClass的每个对象添加到地图视图的注释中.使用调试器,我可以看到,一旦完成所有这些添加,[self.MapView annotations]顺序与NSMutableArray相同.
现在我在mapView中设置另一个断点:viewForAnnotation:并查看1)我的NSMutableArray和2)[self.MapView annotations]的顺序.阵列当然是以相同的顺序.但是,注释的顺序已被扰乱.
这对我来说是个大问题,因为我需要使用用户在下一个视图中选择的特定MyClass实例.AKA,我想查看注释,找到它的索引,然后使用它来获取数组中的相同索引.
我现在意识到我可以直接保存注释(来自Android背景,这对我来说非常酷).但是,我仍然在概念上不知道为什么订单变得混乱.有人能帮我吗?代码如下:
- (void)viewDidLoad
{
if([fromString isEqualToString:@"FromList"])
self.navigationItem.hidesBackButton = TRUE;
else {
self.navigationItem.rightBarButtonItem = nil;
}
self.array = [MySingleton getArray];
//set up map
//declare latitude and longitude of map center
CLLocationCoordinate2D center;
center.latitude = 45;
center.longitude = 45;
//declare span of map (height and width in degrees)
MKCoordinateSpan span;
span.latitudeDelta = .4;
span.longitudeDelta = .4;
//add center and span to a region,
//adjust the region to fit in the mapview
//and assign to …Run Code Online (Sandbox Code Playgroud) 我正在制作一个iPhone应用程序,并希望在导航栏的titleview位置显示一个徽标.我已经创建了UINavigationController的子类,并在viewDidLoad中添加了以下行:
self.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
Run Code Online (Sandbox Code Playgroud)
(这似乎只是浪费一条线,但我在多个地方使用它,我不想出于各种原因使用[UINavigationBar外观])
这适用于导航控制器的直接孩子 - 我看到了徽标.但是,当我将segue推到另一个视图时,徽标不会延续,即使它们(据我所知)是"相同"导航控制器的一部分.
(供参考) - > MyNavigationController - > SomeViewController - > SomeOtherViewController
我也尝试使用以下命令进入SomeOtherViewController的viewDidLoad:
self.navigationController.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
Run Code Online (Sandbox Code Playgroud)
这仍然没有显示徽标.
经过一番探讨,我发现如果1)我在导航控制器的viewDidLoad中没有该行,2)我在SomeOtherViewController的viewDidLoad中有第二行,那么:
开始:查看SomeViewController.没有标志.点击推送segue:徽标现在出现在SomeViewController的栏上,然后消失.我现在正在查看SomeOtherViewController:它没有徽标.单击返回:SomeViewController的导航栏仍然具有徽标.
显然我误解了导航控制器的工作原理.谁能告诉我如何解决这个问题?
我一直在Xcode中开发一个iPhone项目,并决定我想把它放在github上.我在github上创建了一个repo,克隆了它,并将我的所有文件都移到了目录中.然后我试图
git add Directory .
Run Code Online (Sandbox Code Playgroud)
我承诺并推动,发现现在repo中唯一的东西就是这个目录.它什么都没有 - 我甚至无法点击它在github文件查看器中为repo打开它.它是文件夹和文件夹名称的图片.我试过了
git add Directory .
git add Directory -A
git commit -a
Run Code Online (Sandbox Code Playgroud)
什么都不会将这些文件添加到目录中.我究竟做错了什么?
我只想举个例子,而不是用语言来尝试:
我有动物类,以及延伸动物的狗,鱼,猫等.
我有三种不同的方法,它们会返回Map<String,List<Dog>>, Map<String,List<Fish>>, Map<String,List<Cat>>.我们将调用这些getDogMap,getCatMap和getFishMap.
我正在编写一个通用方法,根据各种参数调用这些方法之一.这是我期望被允许做的事情:
public void <A extends Animal> doSomething(){
Map<String,List<A>> someMap;
if(someCondition){
someMap = getDogMap();
}else if(anotherCondition){
someMap = getFishMap();
}else{
someMap = getCatMap():
}
}
Run Code Online (Sandbox Code Playgroud)
或至少,与铸造ala someMap = (Map<String,List<Dog>>) getDogMap();
但是,这不起作用.Eclipse告诉我"Type mismatch: cannot convert from Map<String,List<Dog>> to Map<String,List<A>>"如果我试图强制演员,它会告诉我"Cannot cast from Map<STring,List<Dog>> to Map<String,List<A>>".
我究竟做错了什么?
编辑:已解决.对不起,伙计们,我发现虽然我的数据加载发生在后台线程中,但解析数据的回调却没有.解析数据花了很长时间,这就是锁定我的线程的原因.
编辑:我注意到我的首要问题(ProgressDialog不再旋转)可能是由于我使用ProgressDialog而不是阻止UI线程的问题引起的.如果是这种情况,我该如何解决?
编辑:澄清一下,这并不会永远锁定整个程序.加载完所有内容后,将取消进度对话框,并启动新活动.我的问题是,当它加载时,整个UI锁定(即progressdialog停止旋转)
TL; DR:doInBackground()中的Thread.sleep()锁定UI线程
我有一个应用程序,当特定活动打开时,开始从后端加载后台的大量数据,例如,与计划相关的大量数据.此信息不会立即使用,但如果用户尝试访问它(即通过单击计划按钮,并启动计划活动),则可以使用此信息.
如果用户在单击计划按钮之前等待一点,则会加载所有数据,计划活动将打开,并显示所有内容.我的问题是,如果他们在数据加载前单击按钮.
我的解决方案是创建一个显示ProgressDialog的ASyncTask,同时定期检查数据是否已完成加载,否则将睡眠.它知道数据是通过一些应用程序范围的布尔变量完成加载的.我的问题是,即使Thread.sleep()在doinbackground()中运行,它仍然锁定UI线程.
我使用的是自定义ASyncTask,定义如下:
public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{
private ProgressDialog pd; //the progress dialog
private String title; //the title of the progress dialog
private String message; //the body of the progress dialog
private Runnable task; //contains the code we want to run in the background
private Runnable postTask; //execute when the task ends
private Context c;
public LoadWithProgressDialog(Context context,String t, String m,Runnable r, Runnable postR){
super();
c = context;
task = r; …Run Code Online (Sandbox Code Playgroud)