我正在尝试我的第一个Android应用程序。经过大量的视频和教程后,我创建了我的第一个应用程序,而不是在模拟器上启动。
即使我看过stackoverflow的各种帖子,但仍无法找到解决我问题的方法。
以下是来自调试器的错误消息: -
1)错误打开跟踪文件没有这样的文件或目录
2)致命的例外:主要
我的清单文件: -
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bijaru.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bijaru.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<activity
android:name="com.example.bijaru.LoggedinActivity"
android:label="@string/title_activity_loggedin" >
</activity>
</application>
我的主要活动文件: -
package com.example.bijaru;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
//import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button login;
EditText username,password;
TextView status;
HttpPost httppost;
StringBuffer stringbuffer;
HttpResponse response;
HttpClient client;
List<NameValuePair> namevaluepair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
status=(EditText)findViewById(R.id.status);
//final Context context = this;
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.login:
login();
break;
}
// Intent intent = new Intent(context, LoggedinActivity.class);
// startActivity(intent);
}
private void login() {
// TODO Auto-generated method stub
try{
client = new DefaultHttpClient();
httppost=new HttpPost("hhtp://127.0.0.1/bijaru/check.php");
namevaluepair = new ArrayList<NameValuePair>(1);
namevaluepair.add(new BasicNameValuePair("username", username.getText().toString().trim()));
namevaluepair.add(new BasicNameValuePair("password", password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));
response=client.execute(httppost);
ResponseHandler<String> responsehandler =new BasicResponseHandler();
final String response=client.execute(httppost,responsehandler);
status.setText(""+response);
if(response.equalsIgnoreCase("sucess"))
{
startActivity(new Intent(MainActivity.this,LoggedinActivity.class));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@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;
}
}
来自Debuger的错误Logcat: -
08-06 19:42:23.274: E/cutils-trace(1334): Error opening trace file: No such file or directory (2)
08-06 19:42:39.095: E/cutils-trace(1350): Error opening trace file: No such file or directory (2)
08-06 19:42:43.125: E/AndroidRuntime(1361): FATAL EXCEPTION: main
08-06 19:42:43.125: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bijaru/com.example.bijaru.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.os.Looper.loop(Looper.java:137)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-06 19:42:43.125: E/AndroidRuntime(1361): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 19:42:43.125: E/AndroidRuntime(1361): at java.lang.reflect.Method.invoke(Method.java:525)
08-06 19:42:43.125: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-06 19:42:43.125: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-06 19:42:43.125: E/AndroidRuntime(1361): at dalvik.system.NativeStart.main(Native Method)
08-06 19:42:43.125: E/AndroidRuntime(1361): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-06 19:42:43.125: E/AndroidRuntime(1361): at com.example.bijaru.MainActivity.addListenerOnButton(MainActivity.java:53)
08-06 19:42:43.125: E/AndroidRuntime(1361): at com.example.bijaru.MainActivity.onCreate(MainActivity.java:44)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.Activity.performCreate(Activity.java:5133)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-06 19:42:43.125: E/AndroidRuntime(1361): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-06 19:42:43.125: E/AndroidRuntime(1361): ... 11 more
答案 0 :(得分:3)
您已将status
声明为TextView
TextView status;
但您尝试将其初始化为EditText
status=(EditText)findViewById(R.id.status);
您需要将xml更改为TextView
或将java更改为EditText
答案 1 :(得分:1)
在您的布局中,您使用了TextView,并且在您尝试将其类型转换为EditText的代码中,它导致Classcast错误。在代码中将EditText更改为TextView或在xml中根据需要将TextView更改为EditText。