好吧,我不知道为什么,但每次我在登录活动中点击提交按钮时,我的应用程序都会出现问题。主要活动工作正常......
我的登录活动:
package de.blender4me.einkaufsliste;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends Activity {
EditText password;
EditText username;
Button submit;
TextView userPass;
String usernameString;
String passwordString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
submit = (Button) findViewById(R.id.buttonSubmitLogin);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
password = (EditText) findViewById(R.id.editTextPassword);
passwordString = password.getText().toString();
username = (EditText) findViewById(R.id.editTextUsername);
usernameString = username.getText().toString();
userPass = (TextView) findViewById(R.id.textPassUser);
LongOperation lo = new LongOperation();
lo.execute();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class LongOperation extends AsyncTask<Void, Void, String> {
private String[] values = null;
@Override
protected String doInBackground(Void... params) {
internetHandler ih = new internetHandler();
values = ih.getList(usernameString,passwordString,"","");
userPass.setText(values[0]);
if(!values[0].equalsIgnoreCase("error")&&!values[0].equalsIgnoreCase("401")&&!values[0].equalsIgnoreCase("404")){
userPass.setText("Yep");
}else{
userPass.setText(values[0]);
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
}
}
我的主要活动:
package de.blender4me.einkaufsliste;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
//Widgets
Button addItem;
Button loginMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//listView
LongOperation lo = new LongOperation();
lo.execute();
//register Widgets
addItem =(Button) findViewById(R.id.addItem);
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
loginMenu = (Button) findViewById(R.id.loginMenu);
loginMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Alert
public void openDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Zur Einkaufsliste hinzufügen");
alert.setMessage(" ");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
//listView erstellen
private class LongOperation extends AsyncTask<Void, Void, String> {
private String[] values = null;
@Override
protected String doInBackground(Void... params) {
internetHandler ih = new internetHandler();
values = ih.getList("TestUser","TestPass","TestValue","");
return null;
}
@Override
protected void onPostExecute(String result) {
//Liste konvertieren
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.listview_item, //Genutztes Layout
values); //Genutzter Array
//listView einstellen
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
我的internetHandler:
package de.blender4me.einkaufsliste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Pattern;
public class internetHandler {
//Einkaufsplaner-API aufrufen
public static String[] getList(String name, String pass, String addValue, String removeValue){
InputStream is = null;
try
{
URL url = new URL("https://einkaufsplaner.blender4me.de/api.php?name="+name+"&pass="+pass+"&addValue="+addValue+"&removeValue="+removeValue);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String Values = null;
for(int i = 0; i < 4; ++i){
Values = in.readLine();
}
in.close();
String[] List = Values.split( Pattern.quote( ";" ) );
return List;
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if ( is != null )
try { is.close(); } catch ( IOException e ) { }
}
String[] error = {"error"};
return error;
}
}
错误:
11-07 21:34:29.474:E / Trace(1223):错误打开跟踪文件:没有这样的 文件或目录(2)11-07 21:34:32.785:D / gralloc_goldfish(1223): 未检测到GPU仿真的仿真器。 11-07 21:34:34.074: D / dalvikvm(1223):GC_CONCURRENT释放260K,10%免费7393K / 8135K, 暂停16ms + 15ms,总计59ms 11-07 21:34:39.964: I / Choreographer(1223):跳过了248帧!申请可能是 在主线程上做了太多工作。 11-07 21:34:40.504: W / dalvikvm(1223):threadid = 12:线程退出未捕获的异常 (组= 0x40a13300)11-07 21:34:40.564:E / AndroidRuntime(1223):致命 EXCEPTION:AsyncTask#2 11-07 21:34:40.564:E / AndroidRuntime(1223): java.lang.RuntimeException:执行时发生错误 doInBackground()11-07 21:34:40.564:E / AndroidRuntime(1223):at android.os.AsyncTask $ 3.done(AsyncTask.java:299)11-07 21:34:40.564: E / AndroidRuntime(1223):at java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273) 11-07 21:34:40.564:E / AndroidRuntime(1223):at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 11-07 21:34:40.564:E / AndroidRuntime(1223):at java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307) 11-07 21:34:40.564:E / AndroidRuntime(1223):at java.util.concurrent.FutureTask.run(FutureTask.java:137)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)11-07 21:34:40.564:E / AndroidRuntime(1223):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 11-07 21:34:40.564:E / AndroidRuntime(1223):at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569) 11-07 21:34:40.564:E / AndroidRuntime(1223):at java.lang.Thread.run(Thread.java:856)11-07 21:34:40.564: E / AndroidRuntime(1223):引起: android.view.ViewRootImpl $ CalledFromWrongThreadException:只有 创建视图层次结构的原始线程可以触及其视图。 11-07 21:34:40.564:E / AndroidRuntime(1223):at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.view.View.requestLayout(View.java:15129)11-07 21:34:40.564: E / AndroidRuntime(1223):at android.view.View.requestLayout(View.java:15129)11-07 21:34:40.564: E / AndroidRuntime(1223):at android.view.View.requestLayout(View.java:15129)11-07 21:34:40.564: E / AndroidRuntime(1223):at android.view.View.requestLayout(View.java:15129)11-07 21:34:40.564: E / AndroidRuntime(1223):at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292) 11-07 21:34:40.564:E / AndroidRuntime(1223):at android.view.View.requestLayout(View.java:15129)11-07 21:34:40.564: E / AndroidRuntime(1223):at android.widget.TextView.checkForRelayout(TextView.java:6309)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.widget.TextView.setText(TextView.java:3547)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.widget.TextView.setText(TextView.java:3405)11-07 21:34:40.564:E / AndroidRuntime(1223):at android.widget.TextView.setText(TextView.java:3380)11-07 21:34:40.564:E / AndroidRuntime(1223):at de.blender4me.einkaufsliste.LoginActivity $ LongOperation.doInBackground(LoginActivity.java:71) 11-07 21:34:40.564:E / AndroidRuntime(1223):at de.blender4me.einkaufsliste.LoginActivity $ LongOperation.doInBackground(LoginActivity.java:1) 11-07 21:34:40.564:E / AndroidRuntime(1223):at android.os.AsyncTask $ 2.call(AsyncTask.java:287)11-07 21:34:40.564: E / AndroidRuntime(1223):at java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305) 11-07 21:34:40.564:E / AndroidRuntime(1223):... 5更多11-07 21:34:42.315:I / Process(1223):发送信号。 PID:1223 SIG:9
答案 0 :(得分:0)
实际上,您正在尝试更新导致此错误的doInBackground()
AsyncTask
的应用用户界面。
所以改变你的代码就好了,
@Override
protected String doInBackground(Void... params) {
internetHandler ih = new internetHandler();
values = ih.getList(usernameString,passwordString,"","");
return values[0];
}
@Override
protected void onPostExecute(String result) {
if(!result.equalsIgnoreCase("error") && !result.equalsIgnoreCase("401") && !result.equalsIgnoreCase("404")){
userPass.setText("Yep");
}else{
userPass.setText(result);
}
}
由于doInBackground()
在工作线程中运行,因此您无法从中访问Android的主应用程序UI线程,为此您必须从在UI线程上运行的onPostExecute()
更新UI部件