我的android程序有错误,我不明白,我检查了XML文件的语法,但我没有看到任何错误。
这里
我的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.twitter/com.example.twitter.SettingsActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
at com.example.twitter.SettingsActivity.onCreate(SettingsActivity.java:48)
以下是错误行:
lblName = (EditText) findViewById(R.id.txt_name);
我的活动:
package com.example.twitter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
public class SettingsActivity extends Activity {
AlertDialogManager alert = new AlertDialogManager(); // Alert Dialog Manager
RefreshManager refresh = new RefreshManager(); // Refresh Manager
SessionManager session; // Session Manager Class
private UsersDAO dao; // Init Posts DAO
private ListAdapter PostAdapter;
private ImageLoader imageLoader;
private EditText lblName, lblUsername, lblDescription, lblEmail, lblLocalisation, lblWebsite;
// Buttons
Button validSignup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account);
dao = new UsersDAO(this); // On se connecte a la BDD
session = new SessionManager(getApplicationContext()); // Session class instance
lblName = (EditText) findViewById(R.id.txt_name);
lblUsername = (EditText) findViewById(R.id.txt_username);
lblDescription = (EditText) findViewById(R.id.txt_description);
lblEmail = (EditText) findViewById(R.id.txt_email);
lblLocalisation = (EditText) findViewById(R.id.txt_localisation);
lblWebsite = (EditText) findViewById(R.id.txt_website);
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
lblName.setText(user.get(SessionManager.NAME));
lblUsername.setText(user.get(SessionManager.USERNAME));
lblDescription.setText(user.get(SessionManager.DESCRIPTION));
lblEmail.setText(user.get(SessionManager.EMAIL));
lblLocalisation.setText(user.get(SessionManager.LOCALISATION));
lblWebsite.setText(user.get(SessionManager.WEBSITE));
// Login button
validSignup = (Button) findViewById(R.id.valid_setting);
// Login button click event
validSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Get username, password from EditText
String upd_name = lblName.getText().toString();
String upd_username = lblUsername.getText().toString();
String upd_description = lblDescription.getText().toString();
String upd_email = lblEmail.getText().toString();
String upd_localisation = lblLocalisation.getText().toString();
String upd_website = lblWebsite.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = sdf.format(new Date());
Integer id_user = 2;
dao.update(id_user, upd_username, upd_email, upd_name, upd_description, upd_website, upd_localisation, d);
// Check if username, password is filled
if( upd_name.trim().length() > 0
&& upd_username.trim().length() > 0
&& upd_description.trim().length() > 0
&& upd_email.trim().length() > 0
&& upd_localisation.trim().length() > 0
&& upd_website.trim().length() > 0 ){
// For testing puspose username, password is checked with sample data
// get the current date
//dao = new UsersDAO(getApplicationContext());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = sdf.format(new Date());
System.out.println("time "+d);
Integer id_user = 2;
dao.update(id_user, upd_username, upd_email, upd_name, upd_description, upd_website, upd_localisation, d);
}else{
// user didn't entered username or password
// Show alert asking him to enter the details
alert.showAlertDialog(SettingsActivity.this, "Update settings fail..", "You must fill all the fields", false);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
refresh.data(getApplicationContext(), true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
XML文件
<EditText
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="80dp"
android:hint="Nom et prénom" />
<Button
android:id="@+id/valid_setting"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="397dp"
android:text="Mettre à jour" />
<TextView
android:id="@+id/lab_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:text="Nom d utilisateur"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/txt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="18dp"
android:ems="10"
android:hint="Nom d'utilisateur" />
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="60dp"
android:text="Nom complet"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="120dp"
android:text="Adresse Email"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/txt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="140dp"
android:ems="10"
android:hint="Adresse Email" />
<TextView
android:id="@+id/TextView03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="180dp"
android:text="Description"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/txt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="200dp"
android:hint="Description" />
<TextView
android:id="@+id/TextView04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="240dp"
android:text="URL du site"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/txt_website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="260dp"
android:hint="URL du site" />
<TextView
android:id="@+id/TextView05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="300dp"
android:text="Localisation"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/txt_localisation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="320dp"
android:hint="Localisation" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="24dp"
android:layout_y="358dp"
android:src="@drawable/refresh" />
</AbsoluteLayout>
答案 0 :(得分:5)
尝试删除您的R.java文件。它将被重生。有时当您在xml中移动项目时,它似乎丢失了引用,因此它仍然认为那里有TextView。
答案 1 :(得分:0)
我已经解决了我的问题:我没有更新我的活动的XML视图:
前
setContentView(R.layout.account);
后
setContentView(R.layout.settings);