Questions tagged «android»

Android是Google的移动操作系统,用于对数字设备(智能手机,平板电脑,汽车,电视,Wear,Glass和IoT)进行编程或开发。对于与Android相关的主题,请使用特定于Android的标签,例如android-intent,android-activity,android-adapter等。对于与开发或编程无关但与Android框架相关的问题,请使用以下链接:https:// android.stackexchange.com。

21
关闭应用程序并在Android上启动主屏幕
我有两个不同的活动。第一个启动第二个。在第二个活动中,我打电话System.exit(0)以强制关闭应用程序,但第一个活动将自动显示,而不是应用程序返回到主屏幕。如何避免这种情况,并使应用程序返回主屏幕?

10
Android中的圆形按钮
我想在Android程序中创建圆形按钮。我看过 如何创建带有圆角的EditText? 我要实现的是: 圆形边缘按钮 在不同状态下更改按钮背景/外观(如Onclick,Focus) 使用我自己的PNG作为背景,而不创建形状。

7
毕加索从文件系统加载图像
我可以使用Picasso库从文件系统加载图像吗? 我startActivityForResult用来让用户从他的画廊中挑选一张照片,然后想要显示所选的图像。 我已经有工作代码来获取图像文件系统Uri,但是无法使该Picasso.load()方法工作。
76 android  picasso 

10
具有过滤器Android的自定义Listview适配器
请尝试在我的列表视图上实施过滤器。但是每当文本更改时,列表就会消失。请帮助这是我的代码。适配器类。 package com.talagbe.schymn; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class HymnsAdapter extends ArrayAdapter<Hymns> { ArrayList<Hymns> hymnarray; Context context; LayoutInflater inflater; int Resource; public HymnsAdapter(Context context, int resource, ArrayList<Hymns> objects) { super(context, resource, objects); // TODO Auto-generated constructor stub hymnarray=objects; Resource= resource; …

5
android资源和资源ID之间的映射如何工作?
对于Android,仅通过R.id.XXX定位正确的资源是神奇的。 AFAIK,资源被编译为二进制格式,那么这种映射逻辑如何在后台运行? 也许它是这样的: 例如,在layout1.xml中,我们得到: <Button android:id="@+id/button1" > 并且AAPT将在R.java中生成此代码: public static final int button1=0x7f05000b; 生成* .apk时,将@ + id / button1替换为“ 0x7f05000b”。 因此,当我们调用: findViewById(R.id.button1); 尽管ID是0x7f05000b之类的数字,但我们基本上仍会根据ID进行搜索。 谢谢! 加 我真正想知道的是,如何将资源id整数解析为资源内容?换句话说,Android运行时如何查找以资源ID为唯一线索的资源内容? 例如,如何找到带有资源ID的可绘制图片?或如何找到带有资源ID的字符串值?

6
创建一个新的可绘制颜色
我正在尝试将十六进制值转换为int,以便创建新的可绘制颜色。我不确定是否可以,但是根据文档,应该可以。它明确地要求 公共ColorDrawable(int颜色) 在API级别1中添加。创建具有指定颜色的新ColorDrawable。 参数 color 绘制的颜色。 因此,我的代码无法正常工作,因为我收到一个无效的int:“ FF6666”错误。有任何想法吗? int decode = Integer.decode("FF6666"); ColorDrawable colorDrawable = new ColorDrawable(decode);

24
NoClassDefFoundError用于Android上的Java库中的代码
我的用户经常遇到错误。该应用程序在启动过程中崩溃。当应该加载MainActivity时,VM显然找不到该类。我不知道为什么。该应用程序的体系结构是有一个通用项目,我的免费版和专业版都在使用。不知道是否相关。请参阅下面的堆栈跟踪。有什么想法吗? java.lang.NoClassDefFoundError: com.android.common.MainActivity at com.mycompany.myapp.Splash.onCreate(Splash.java:23) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: com.android.common.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.android.pro-1.apk] 编辑: 感谢您在下面的评论,理查德。现在我换com.android.Splash了别的东西。无论如何,它不是真正的类名。我的错...!

24
Android TextView文本未换行
谁能告诉我文本出了什么问题?长于一行的文本不会自动换行到下一行,而是超出屏幕范围。 以下是代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dip"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dip"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dip"> <TextView android:id="@+id/reviewItemEntityName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="event/venue" android:textColor="@color/maroon" android:singleLine="true" android:ellipsize="end" android:textSize="14sp" android:textStyle="bold" android:layout_weight="1" /> <ImageView android:id="@+id/reviewItemStarRating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:src="@drawable/title_1_star" /> </LinearLayout> <TextView android:id="@+id/reviewItemDescription" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Description comes here" android:textSize="12sp" android:layout_weight="1"/> …
76 android  textview 

6
即使活动已销毁,AsyncTask也不会停止
我有一个AsyncTask对象,该对象在创建活动时开始执行,并在后台执行操作(最多下载100张图片)。一切正常,但有这种特殊的行为我无法理解。 例如:当android屏幕的方向改变时,活动将被销毁并重新创建。因此,我重写了onRetainNonConfigurationInstance()方法并将所有下载的数据保存在AsyncTask中执行。我这样做的目的是每次在方向更改期间破坏活动创建时都不要运行AsyncTask,但是正如我在日志中看到的那样,以前的AsyncTask仍在执行。(尽管数据已正确保存) 我什至尝试在活动的onDestroy()方法中取消AsyncTask,但日志仍显示AsyncTask正在运行。 这确实是一种奇怪的行为,如果有人可以告诉我停止/取消AsyncTask的正确过程,这将非常感谢。 谢谢

2
finish()之后onCreate流继续
我想从onCreate方法内部完成一项活动。当我调用时finish(),onDestroy()没有立即调用,代码不断流过finish()。onDestroy()直到onCreate()大括号结束才被调用。 根据onCreate()developer.android.com/reference上的描述。 您可以从此函数内调用finish(),在这种情况下,将立即调用onDestroy(),而不会执行其余的活动生命周期(onStart(),onResume(),onPause()等)。 我问的原因是:我想检查传递给的捆绑包中的数据onCreate()。当然,我可以控制传递给的内容onCreate,但我仍然认为应该在交付时进行检查。 我的代码包含A启动Activity的类B。我相信不应调用最后两个“ if子句之外”标签,因为语句中的finish方法if应该已经破坏了活动。它与if子句无关,因为第二个finish()调用之后的标记行仍然被读取。 我的代码: A级 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // goToBButton: when pressed sends message to class B. Button goToBButton = (Button)this.findViewById(R.id.go_to__b_btn); goToBButton.setOnClickListener(new OnClickListener() { @Override public void onClick (View v) { Log.i(TAG,"A Class: goToBButton, onClick"); Intent i = new Intent(A.this, B.class); startActivityForResult(i,REQ_TO_B); …
76 android  oncreate 

12
如何在Android Marshmallow上使用旧版Apache HTTP客户端?
背景 在Android Marshmallow上,Google已完全删除了对Apache HTTP客户端的支持(链接此处),因为与其他替代品相比,它的性能不佳。 这也可能是导致Android Marshmallow上许多应用程序崩溃的原因。 问题 通过将以下行添加到gradle文件中,Google允许您仍然使用此API,而不是内置API: useLibrary 'org.apache.http.legacy' 所以,这就是我所做的: dependencies { classpath 'com.android.tools.build:gradle:1.3.0' } 和: android { compileSdkVersion 'android-MNC' buildToolsVersion "23.0.0 rc3" useLibrary 'org.apache.http.legacy' defaultConfig { applicationId "com.example.user.androidmtest" minSdkVersion 'MNC' targetSdkVersion 'MNC' versionCode 1 versionName "1.0" } 当我尝试它时,它可以很好地编译(没有错误显示,并且我可以运行概念验证应用程序,因为它没有任何特殊的代码),但是当我尝试使用一些我知道的类时,是旧版API(例如“ HttpClient”类)的一部分,我发现它不允许这样做。 我知道不建议您使用此解决方案,但是我们必须至少暂时准备好该应用程序,直到我们100%应对Android Marshmallow应当更改的所有事情,并且我们不希望出现意外情况崩溃的形式。 这是屏幕截图: 问题 为什么会发生?我使用正确了吗? 编辑:在此报告有关此问题的信息: https://code.google.com/p/android/issues/detail?id=181474

7
在Android上写入外部SD卡的通用方法
在我的应用程序中,我需要在设备存储中存储大量图像。这样的文件往往可以满足设备的存储需求,我希望用户可以选择外部SD卡作为目标文件夹。 我到处都读到Android不允许用户写入外部SD卡,所谓SD卡,是指外部可安装的SD卡,而不是外部存储,但是文件管理器应用程序设法在所有Android版本上写入外部SD。 在不同的API级别(Pre-KitKat,KitKat,Lollipop +)上授予对外部SD卡的读/写访问权限的更好方法是什么? 更新1 我从Doomknight的答案中尝试了方法1,但无济于事:如您所见,在尝试在SD上写入之前,我正在运行时检查权限: HashSet<String> extDirs = getStorageDirectories(); for(String dir: extDirs) { Log.e("SD",dir); File f = new File(new File(dir),"TEST.TXT"); try { if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED) { f.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } } 但是我收到访问错误,尝试在两种不同的设备上进行:HTC10和Shield K1。 10-22 14:52:57.329 30280-30280/? E/SD: /mnt/media_rw/F38E-14F8 10-22 14:52:57.329 30280-30280/? W/System.err: java.io.IOException: open failed: EACCES …

11
为什么从GCM服务器端获取“ MismatchSenderId”?
我正在尝试为我的Android应用创建推送服务,并且在此问题上遵循Google GCM的文档和示例: 我可以注册/注销我的Android应用程序。从服务器端,我可以看到已经注册了一个订阅,但是当我尝试将消息从服​​务器发送到我的Android应用程序时,总是出现以下错误: 向设备#0发送消息时出错:MismatchSenderId 对于我的Android应用,我使用SENDER_ID = 200000000001。 对于Google所说的服务器端,我使用API​​_KEY = AIzxxxxxxxxxxxxxxxxXxxXxxXxxxXXXXXxxxxxxs 我遵循了这份文件: http://developer.android.com/guide/google/gcm/demo.html 我也在Google API面板中启用了GCM,但我会收到该烦人的错误消息。 我该如何解决这个问题?

8
所有标准的Android图标资源在哪里?
我刚刚开始编写我的第一个Java / Android应用程序,因此请原谅所有n00b问题。 如何访问标准操作系统图标,例如搜索图标/菜单图标等?它们不在res / drawable文件夹中,我希望在其中找到它们。它们是否可以作为SDK的一部分使用,还是必须单独下载? 编辑 也许您可以进一步帮助我-我收到以下错误(重复出现): [2011-09-08 19:59:47 - TweetTab] C:\Users\Dan\workspace\TweetTab\res\menu\options_menu.xml:4: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_menu_search'). 但是,图标不是空的,xml如下所示: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:icon="@drawable/ic_menu_search" android:title="@string/search"/> </menu> 我想念什么吗? 解决了 我更改android:icon="@drawable/ic_menu_search"为android:icon="@android:drawable/ic_menu_search",然后可以找到该图标。
76 java  android 

4
如何创建Looper线程,然后立即向其发送消息?
我有一个工作线程位于后台,用于处理消息。像这样: class Worker extends Thread { public volatile Handler handler; // actually private, of course public void run() { Looper.prepare(); mHandler = new Handler() { // the Handler hooks up to the current Thread public boolean handleMessage(Message msg) { // ... } }; Looper.loop(); } } 从主线程(UI线程,没关系),我想做这样的事情: Worker worker = …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.