除非我删除这个,否则我会收到例外:
机器人:targetSdkVersion = "15"
我在SO的另一个帖子中找到了.
但是,我已经在那里使用了targetSdkVersion运行了几天.这是我的代码:
public class MainActivity extends BaseActivity {
private TextView textView;
private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.todaysReport);
image = (ImageView) findViewById(R.id.dangerRose);
fetcher task = new fetcher();
task.execute();
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src", src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap", "returned");
return …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android应用程序,它使用在.ashx请求处理程序中开发的Web服务.
这是一些代码.
public class Home extends Activity implements AdapterView.OnItemClickListener {
/** Called when the activity is first created. */
Context context = Home.this;
HashMap<String, String> map = new HashMap<String, String>();
ArrayList<String> BookTitle = new ArrayList<String>();
ArrayList<String> BookCoverPhotos = new ArrayList<String>();
URL bookImageURL = null;
Bitmap bitMapImage = null;
View homeTabLayout;
View reviewLayout;
ArrayList<String> ImageUrl = new ArrayList<String>();
ImageButton btnBack;
// XML Parsing
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// All static variables
static final String URL = …Run Code Online (Sandbox Code Playgroud) 是否可以将文件传递给AsyncTask?像这样?
public class UploadImagesTask extends AsyncTask<String, String, File> {
@Override
protected File doInBackground(String... params) {
Run Code Online (Sandbox Code Playgroud)
这样做我会访问该文件吗?
params[2]
Run Code Online (Sandbox Code Playgroud) 我正在扩展AsyncTask并在我的doInBackground()中使用findViewById得到一个按钮视图,当我调用button.performClick()时,我的应用程序崩溃了.
知道为什么吗?
这是logcat:
E/AndroidRuntime(604): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(604): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(604): at android.os.AsyncTask$3.done(AsyncTask.java:278)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(604): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime(604): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime(604): at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime(604): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
E/AndroidRuntime(604): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
E/AndroidRuntime(604): at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:3610)
E/AndroidRuntime(604): at android.view.View.playSoundEffect(View.java:13103)
E/AndroidRuntime(604): at android.view.View.performClick(View.java:3510)
Run Code Online (Sandbox Code Playgroud) 我有一个Async正在运行以从我创建的页面获取数据.它得到的文本很好,但当我尝试通过另一个类从图像src获取图像时,应用程序强制关闭.以下是强制关闭的代码:
public class FullReportActivity extends NavigationActivity {
private TextView textView;
private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
private ImageView ivDangerRose;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// tell which region this covers
getSupportActionBar().setSubtitle("...from Sierra Avalanche Center");
setContentView(R.layout.activity_fullreport);
textView = (TextView) findViewById(R.id.todaysReport);
ivDangerRose = (ImageView) findViewById(R.id.dangerRose);
fetcher task = new fetcher();
task.execute();
}
// GET THE IMAGE and RETURN IT
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect(); …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的应用程序,主要威胁启动2 AsyncTasks.2个AsyncTasks几乎相同,这是我的一个AsyncTasks的代码:
private class waitPlayer extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
CreateLog.addToLog(e.toString);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Update your layout here
super.onPostExecute(result);
setCanPlay(true, lastPlayerPlayed);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//progress_p1.setProgress((int) progress[0]);
//progress_p2.setProgress((int) progress[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
AsyncTask唯一能做的就是等待200ms,完成后它会调用一个方法.该方法将再次调用AsyncTask.另一个AsyncTask几乎相同,只等待3000ms并调用另一种方法.
问题是,在我添加第二个AsyncTask之后,我的应用程序变得很慢.它在AsyncTasks运行时冻结几秒钟.我的Logcat说充满了垃圾收集器通知.
我的手机没有冻结,只有应用程序.我有大约200mb的免费RAM.
01-06 19:26:00.695: W/ActivityThread(25739): Application com.obattech.twoplayergame is waiting for the debugger on port 8100...
01-06 19:26:00.705: I/System.out(25739): Sending WAIT chunk
01-06 19:26:00.710: …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个AsyncTask类,但我注意到无论传递给onPostExecute函数的结果是什么,它仍会指示应用程序转到else {},即使它完全符合if语句.为什么会这样?
这是我正在运行的AsyncTask:
class CheckPassword extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ChooseTable.this);
pDialog.setMessage("Checking Password. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... p) {
String password = p[0];
int success;
String access = "";
try {
// Building Parameter
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("password", password));
//ipaddress of server
Intent i = getIntent();
Bundle b = i.getExtras();
ipaddress = b.getString("IP_Address");
if(ipaddress != "" || ipaddress != "...:")
{
// single …Run Code Online (Sandbox Code Playgroud) 我是android的新手,我想获取联系人并在列表视图中显示.这是我在asynctask.please中获取联系人的代码,帮助我们在获取每个联系人后更新列表视图.
protected Void doInBackground(Void... params) {
ContentResolver cr = ReadContactsActivity.this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//li.add("Name:"+name+",Number:"+phone);
publishProgress("Name:"+name+",Number:"+phone);
}
pCur.close();
}
}
}
return null;
}
// A callback method executed on UI thread, invoked by the publishProgress() …Run Code Online (Sandbox Code Playgroud) 在这里第一次发布问题.刚开始编程和编码我的第一个应用程序
我有一个Application类文件,其方法为fetchUpdates():
public synchronized void fetchUpdates() {
String[] mURLs = getURLs();
new DownloadJSONData().execute(mURLs[0], mURLs[1]);
}
Run Code Online (Sandbox Code Playgroud)
DownloadJSONData是asyncTask从服务器获取更新,该onPostExecute方法使用服务器中的JSON数组更新sqlite数据库.
应用程序的不同组件(小部件和活动)调用此方法以从服务器获取更新以更新数据库.
问题:synchronized方法是否在与UI线程不同的线程上运行?如果是这样,DownloadJSONData将同步fetchUpdates()方法中的代码移动到右边应该没有问题吗?如果在连接服务器或从服务器下载数据时存在滞留,它不应该阻止UI线程吗?
动机:我正在尝试更改fetchUpdates()方法以返回指示数据库是否已更新的布尔值.然而,因为它现在是(我认为)fetchUpdates()方法之前完成asyncTask的onPostExecute方法,因此,fetchUpdates()方法不能指明呼叫是否DownloadJSONData()更新数据库.fetchUpdates()如果在调用后更新数据库,我需要调用方法的应用程序组件以不同的方式运行.
PS因为我刚接触编程简单而详细的解释会非常有用.
谢谢!
我正试图在我的应用程序中解析json:
所以首先我为我的Android应用程序创建了常量类,它为app.config提供了大约6个变量:
(等级:1)
public class Constants{
// url to make request
public static String url = "http://server.com/";
// JSON Node names
public static final String TAG_CONTACTS = "contacts";
public static final String TAG_ID = "id";
public static final String TAG_NAME = "name";
public static final String TAG_EMAIL = "email";
public static final String TAG_ADDRESS = "address";
public static final String TAG_GENDER = "gender";
}
Run Code Online (Sandbox Code Playgroud)
现在我想在不同的类中使用它,所以我继续创建了不同的类:
(等级:2)
public class ReadFiles{
public void readConstant(){
//appConfig is JSONArray
JSONArray appConfig = null;
// …Run Code Online (Sandbox Code Playgroud) android ×10
java ×6
bitmap ×1
crash ×1
if-statement ×1
performance ×1
synchronized ×1
web-services ×1