我试图在我的侦听器类中通过ID获取TextView,它处理一个微调器的onItemSelected事件.在我的主Activity中,我可以调用findViewById(R.id.textView1),它工作正常.但是如果我在我的spinner监听器类中调用相同的函数,它总是返回null并强制关闭应用程序.
任何人都知道为什么findViewById在我的主Activity中工作但在我的监听器类中不起作用?
这是我主要活动的代码.单击按钮时调用它:
public void something ()
{
setContentView(R.layout.spinnerLayout);
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Run Code Online (Sandbox Code Playgroud)
这是MyOnItemSelectedListener的代码:
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
TextView tv = view.findViewById(R.id.myTextView);
// I want to set the text of this text view to the value selected using the spinner
tv.setText("Something");
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能获得"TextView tv …
如何使用Delphi从正在运行的Chrome实例中获取网址?
我正在尝试使用Delphi应用程序来获取浏览器的活动选项卡的URL(IE,Mozilla等).我正在使用适用于IE的代码:
procedure TForm1.GetCurrentURL (var URL, Title : string);
var
DDEClient : TDDEClientConv;
s : string;
begin
s := '';
try
DDEClient := TDDEClientConv.Create(self);
with DDEClient do
begin
if SetLink('IExplore','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Netscape','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Mosaic','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Netscp6','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Mozilla','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle')
else
if SetLink('Firefox','WWW_GetWindowInfo') then
s := RequestData('0xFFFFFFFF,sURL,sTitle');
end;
if s <> '' then
begin
delete(s,1,1);
URL := copy(s,1,pos('","',s)-1); …Run Code Online (Sandbox Code Playgroud) 我有一个下拉列表,在某些情况下需要删除一个项目(在代码隐藏中).我需要根据项目的值删除项目.
我怎样才能做到这一点?
我正在使用Python在系统级Linux上做一个项目.所以,我想知道,如果我作为普通用户运行我的代码,如果我正在访问系统文件,那么它应该具有root权限,那么我如何提示root密码并以超级用户身份运行更多代码.我想知道,如何以超级用户身份运行python脚本,并提供密码提示.
任何帮助将不胜感激.先感谢您..
我试图从表中选择一个名为"YEAR"的列.
我可以使用以下方法实现此目的:
SELECT * FROM OPENQUERY (mybook, 'SELECT mytable.year FROM mytable')
Run Code Online (Sandbox Code Playgroud)
但是当我使用它时不是:
SELECT * FROM OPENQUERY (mybook, 'SELECT year FROM mytable')
Run Code Online (Sandbox Code Playgroud)
这是因为"年"是某种保留字还是有其他原因?
运行以下行后:
double d=Float.parseFloat("9.99");
System.out.println(""+d);
Run Code Online (Sandbox Code Playgroud)
我得到了这个:9.989999771118164
我期待看到:9.99
为什么以及如何解决这个问题?
================================================== =====
编辑:我应该使用的是:double d = Double.parseDouble("9.99")打印出9.99.这是我的错,但看到详细的答案很有意思,谢谢!
我已经忙了几天试图找出如何在我的Cocos2d项目中处理触摸.情况与正常情况有点不同.我有几个不同的游戏图层,上面有我需要通过触摸控制的项目:
现在我在ControlLayer中工作得很好,我可以移动我的可玩角色并让他跳跃并做其他愚蠢的事情.然而,我无法掌握如何实现我的一些CCSprites的触摸.
到目前为止我收集的信息让我觉得我需要从控制层获取所有触摸输入.然后我不知何故需要将触摸信息"级联"到GameplayLayer,以便我可以在那里处理输入.另一种选择是让我通过某种方式创建一个指向应该可触摸的对象的指针来从我的精灵中获取CGRect信息.我应该能够在ControlLayer中使用该信息来检查该项目中每个项目是否被触摸.
这样做的最佳选择是什么,我该如何实现?我对使用cocoa和Objective C进行编程有点新意,所以我不确定这个语言的最佳选择是什么,以及如何在另一个类中访问sprite CGRect信息([mySpriteName boundingBox])然后是它的层呈现在.
目前,我确定让它工作的唯一方法是为每个CCSprite位置创建重复的CGRects,因此我可以检查它们,但我知道这不是正确的方法.
我到目前为止(测试)是这样的:ControlLayer.m
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGRect rect = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
//Tried some stuff here to get see if I could get a sprite by tagname so I could use it's bounding box but that didn't work
// Check for touch with specific location
if (CGRectContainsPoint([tree boundingBox], location)) {
CCLOG(@"CGRect contains the location, touched!");
} …Run Code Online (Sandbox Code Playgroud) 我在今天工作的代码库中遇到了一个模式,最初看起来非常聪明,后来让我疯了,现在我想知道是否有办法拯救聪明的部分,同时尽量减少疯狂.
我们有一堆实现的对象IContractObject,以及一个如下所示的类InvariantChecker:
internal class InvariantChecker : IDisposable
{
private IContractObject obj;
public InvariantChecker(IContractObject obj)
{
this.obj = obj;
}
public void Dispose()
{
if (!obj.CheckInvariants())
{
throw new ContractViolatedException();
}
}
}
internal class Foo : IContractObject
{
private int DoWork()
{
using (new InvariantChecker(this))
{
// do some stuff
}
// when the Dispose() method is called here, we'll throw if the work we
// did invalidated our state somehow
}
}
Run Code Online (Sandbox Code Playgroud)
这用于提供状态一致性的相对无痛的运行时验证.我没有写这个,但它最初似乎是一个非常酷的主意.
但是,如果Foo.DoWork …