小编cod*_*gic的帖子

哪个是添加按钮的最佳方式?

我是android开发的新手.我有一个疑问.我知道你可以添加一个按钮并初始化它

Button b1=(Button) findViewById(R.id.button1);
Run Code Online (Sandbox Code Playgroud)

我还可以在XML文件中给出一个名称.

  android:onClick="click_event"
Run Code Online (Sandbox Code Playgroud)

我怀疑,这是最好和最有效的方式吗?就像它说使用@string资源而不是硬编码资源更好.

android android-button

3
推荐指数
1
解决办法
9369
查看次数

Android - ListView - 上下文菜单不起作用

我有一个ListFragment我使用注册上下文菜单的地方registerForContextMenu(),我覆盖了onCreateContextMenu(). 问题是onCreateContextMenu()当我长按任何项目时从未调用过。

这是一些代码:

public class List_F extends ListFragment {
    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);

        registerForContextMenu(getListView());

        setListAdapter(...);
        setListShown(false);

        // launch cursor loader
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int i = item.getItemId();
        if (i == R.id.menu_item_delete) {
            delete(info.id);
            return true;
        } else {
            return super.onContextItemSelected(item); …
Run Code Online (Sandbox Code Playgroud)

android android-contextmenu

3
推荐指数
1
解决办法
2693
查看次数

如何在Java中读取扩展文件属性?

我有扩展属性的文件 user.MYATTR

当我运行命令:时getfattr fileName -d,我得到了:

user.MYATTR="attribute_value"
Run Code Online (Sandbox Code Playgroud)

我的操作系统是Red Hat Enterprise Linux Server 6.4版(圣地亚哥)

但是当我运行我的Java代码时

    LOG.debug("readExtentedAttribute(path={},name={}", path, name);

    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);

    ByteBuffer buffer = ByteBuffer.allocate(view.size(name));
    view.read(name, buffer);
    buffer.flip();
    String value = Charset.defaultCharset().decode(buffer).toString();
    LOG.trace("extended attribute value = {}", value);
Run Code Online (Sandbox Code Playgroud)

我正在例外

java.nio.file.FileSystemException: /opt/myfolder/myfile: Unable to get size of extended attribute 'user.MYATTR': No data available
    at sun.nio.fs.LinuxUserDefinedFileAttributeView.size(LinuxUserDefinedFileAttributeView.java:141) ~[na:1.7.0_25]
    at sk.tempest.cda.impex.service.FileServiceImpl.readExtentedAttribute(FileServiceImpl.java:39) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na]
    at sk.tempest.cda.impex.service.TapeMediumServiceImpl.hasDissemFlag(TapeMediumServiceImpl.java:189) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na]
    at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
    at …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
1
解决办法
1890
查看次数

Android TextView.SetText(int resid)如何工作?

这是Big Nerd Ranch指南中的一个例子:

package com.example.geoquiz;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question_africa, true),
            new TrueFalse(R.string.question_americas, false),
            new TrueFalse(R.string.question_asia, false),
            new TrueFalse(R.string.question_mideast, true),
            new TrueFalse(R.string.question_oceans, true)
    };
    private int mCurrentIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        mTrueButton = (Button)findViewById(R.id.true_button);
        mFalseButton = …
Run Code Online (Sandbox Code Playgroud)

java android textview android-resources

3
推荐指数
1
解决办法
3926
查看次数

如何防止我的CountDownTimer在后台运行?

我正在制作一款Android应用程序,它可以为您获得的积分数量设置时间限制.但是如果你关闭应用程序,计时器会继续运行.如何暂停CountDownTimer应用暂停时的暂停状态?

java android countdowntimer

3
推荐指数
1
解决办法
969
查看次数

更改android /JAVA中变量的值

我试图制作一个简单的应用程序,其中单击tapb Button增加 notaps 的变量值,重置Button将其设置为 0。当我单击tapb它时,它会增加值并单击reset重置它,但是当我再次单击tabp它时,它会从以前的值开始增加。

例如:

init value of notaps = 0;
Run Code Online (Sandbox Code Playgroud)

我点击tabp3 次,notaps价值 = 3

我点击resetnotaps值 = 0

我点击tabp3 次,notaps价值 = 4

    Button tapb = (Button)findViewById(R.id.tapb);
    Button reset = (Button)findViewById(R.id.reset);


    tapb.setOnClickListener(
            new Button.OnClickListener(){
                int notaps = 0;
                @Override
                public void onClick(View v) {
                    TextView taps = (TextView)findViewById(R.id.taps);
                    notaps++;
                    taps.setText(String.valueOf(notaps));

                }
            }
    );

    reset.setOnClickListener(
            new Button.OnClickListener() {

                @Override …
Run Code Online (Sandbox Code Playgroud)

java android button textview onclicklistener

3
推荐指数
1
解决办法
1824
查看次数

图像按钮无法强制转换为窗口小部件按钮

我尝试了类似堆栈溢出的以下内容Clean,Build Project Restart Eclipse

我也试过更新所有内容:从SDK和Eclipse插件重启模拟器等

Log Cat输出:

09-26 22:11:49.182: E/AndroidRuntime(1393): FATAL EXCEPTION: main
09-26 22:11:49.182: E/AndroidRuntime(1393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ass2/com.example.ass2.MP3Player}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.os.Looper.loop(Looper.java:137)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 22:11:49.182: E/AndroidRuntime(1393):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) …
Run Code Online (Sandbox Code Playgroud)

java eclipse android

2
推荐指数
1
解决办法
5641
查看次数

2
推荐指数
1
解决办法
6514
查看次数

使用内联javascript添加当前日期

运行以下代码时,输​​出中没有显示日期.我似乎无法弄清楚为什么约会不会出现......

<!DOCTYPE html>
<html>
<head>
 <title>document.write() Example</title>
</head>
<body>
 <p>The current date and time is:
 <script type=”text/javascript”>
 document.write(“<strong>” + (new Date()).toString() + “</strong>”);
 </script>
 </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript date

2
推荐指数
1
解决办法
4194
查看次数

AsyncTask只适用于调试?

奇怪的事发生了.我有一个AsyncTask运行,所以我不在UI上获取数据库数据Thread.这是我的任务:

ProductListActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);
    if (savedInstanceState == null) {
        new QueryProductsTask(this).execute();
    }
}

public void populateProductList(Cursor products) {
    ProductListAdapter productListAdapter =
            new ProductListAdapter(this, R.layout.product_list_row, products);
    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(productListAdapter);
}
Run Code Online (Sandbox Code Playgroud)

QueryProductsTask.java:

protected Cursor doInBackground(Void... params) {
    android.os.Debug.waitForDebugger();
    Log.v("BagIt.AsyncTask", "doInBackground running");
    // Fetches info from database:
    return new Products(activity.getBaseContext()).select(null, null, null, null);
}

protected void onPostExecute(Cursor c) {
    android.os.Debug.waitForDebugger();
    Log.v("BagIt.AsyncTask", "onPostExecute running");
    // Populates ListView through a custom adapter …
Run Code Online (Sandbox Code Playgroud)

java android android-asynctask

2
推荐指数
1
解决办法
979
查看次数