我从MySQL填充了ListView
。到目前为止是好的和有效的。我在数据库中有4行,我得到了它们。所以现在我尝试用户从此ListView
中选择项目来打开此特定项目并从数据库加载更多信息。例如:
所以我有第一个屏幕完成并正常工作。但现在当我点击Item 1
时,我会在NULL
LogCat
11-07 11:55:30.805: W/Bundle(1393): Key id expected Integer but value was a java.lang.Long. The default value 0 was returned.
11-07 11:55:30.835: W/Bundle(1393): Attempt to cast generated internal exception:
11-07 11:55:30.835: W/Bundle(1393): java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
11-07 11:55:30.835: W/Bundle(1393): at android.os.Bundle.getInt(Bundle.java:1000)
11-07 11:55:30.835: W/Bundle(1393): at android.os.Bundle.getInt(Bundle.java:982)
11-07 11:55:30.835: W/Bundle(1393): at com.reserveme.RestaurantInformation.onCreate(UserInformation.java:53)
11-07 11:55:30.835: W/Bundle(1393): at android.app.Activity.performCreate(Activity.java:5231)
11-07 11:55:30.835: W/Bundle(1393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 11:55:30.835: W/Bundle(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-07 11:55:30.835: W/Bundle(1393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-07 11:55:30.835: W/Bundle(1393): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-07 11:55:30.835: W/Bundle(1393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-07 11:55:30.835: W/Bundle(1393): at android.os.Handler.dispatchMessage(Handler.java:102)
11-07 11:55:30.835: W/Bundle(1393): at android.os.Looper.loop(Looper.java:136)
11-07 11:55:30.835: W/Bundle(1393): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-07 11:55:30.835: W/Bundle(1393): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 11:55:30.835: W/Bundle(1393): at java.lang.reflect.Method.invoke(Method.java:515)
11-07 11:55:30.835: W/Bundle(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-07 11:55:30.835: W/Bundle(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-07 11:55:30.835: W/Bundle(1393): at dalvik.system.NativeStart.main(Native Method)
以下是我正在尝试的内容 - > User.java
a.k.a Activiti A
public class Users extends Activity {
ListView listView;
TextView textView;
private StockAdaptor stockAdaptor;
String jsonResult = null;
ImageView image;
String info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users); //Just a listView, shown below
listView = (ListView) findViewById(android.R.id.list);
textView = (TextView) findViewById(R.id.name);
textView = (TextView) findViewById(R.id.rest_id);
textView = (TextView) findViewById(R.id.info);
image = (ImageView) findViewById(R.id.image);
new JsonReadTask().execute("http://link.to.site/GetUsers.php");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(listView.getLayoutParams());
lp.setMargins(10, 10, 0, 0);
listView.setLayoutParams(lp);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
Intent intent = new Intent(Users.this, UserInformation.class);
//intent.putExtra("id", position);
intent.putExtra("id", stockAdaptor.getItemId(position));
intent.putExtra("info", info);
//intent.putExtra("text", text);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true; //No options
}
public void onStart() {
super.onStart();
stockAdaptor = new StockAdaptor(this); //Create a new StockAdaptor
}
public static String strFromStream(InputStream in) throws IOException { //Simple function, getting a String from an InputStream
StringBuilder out = new StringBuilder();
BufferedReader breader = new BufferedReader(new InputStreamReader(in));
String cline;
String newLine = System.getProperty("line.separator");
while ((cline = breader.readLine()) != null) {
out.append(cline);
out.append(newLine);
}
return out.toString();
}
private class StockAdaptor extends BaseAdapter { //The stocks list adaptor
class ViewHolder {
TextView id;
TextView name;
TextView info;
ImageView image;
}
private LayoutInflater layoutInflater;
private RestaurantStrings[] stocks = null; //Array of stocks
private ListView stocksListView = null;
public StockAdaptor(Context context) {
super();
layoutInflater = LayoutInflater.from(context);
}
public void setStockList(RestaurantStrings[] stocksinfo) {
this.stocks = stocksinfo;// //////////////LITERALLY THIS
}
@Override
public int getCount() {
return stocks.length;
}
@Override
public Object getItem(int position) {
return stocks[position];
}
public RestaurantStrings[] getAll() { //Return the array of stocks
return stocks;
}
@Override
public long getItemId(int position) {
return stocks[position].id;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder; //New holder
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.users_second, null);
holder = new ViewHolder();
// Creates the new viewHolder define above, storing references to the children
holder.id = (TextView) convertView.findViewById(R.id.rest_id);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.info = (TextView) convertView.findViewById(R.id.info);
if (holder.image != null) {
if (holder.image.getDrawable() == null) {
new ImageDownloaderTask(holder.image, null)
.execute(stocks[position].image); //Download the image using the image
}
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//holder.id.setText(stocks[position].id);
holder.name.setText(stocks[position].name);
holder.info.setText(stocks[position].info);
return convertView;
}
}
// some more code below ( image download, json response ... etc)
这是UserInformation
a.ka. Activity B
public class UserInformation extends Activity {
String info;
int rest_id;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_informations);
textView = (TextView) findViewById(R.id.rest_id);
textView = (TextView) findViewById(R.id.info);
Bundle b = getIntent().getExtras();
if (b != null) {
rest_id = b.getInt("id");
textView.setText(rest_id+"");
info = b.getString("info");
textView.setText(info+"");
}
}
user_information.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="13.07"
android:background="@drawable/restaurants_buttons"
android:orientation="horizontal" >
<TextView
android:id="@+id/info"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="" />
<TextView
android:id="@+id/rest_id"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="15dp" />
</LinearLayout>
</LinearLayout>
目前,我很高兴只展示Info
。
我很抱歉很长的帖子,但我试着整天解决这个问题而且没有运气。我不知道还能做什么。也可能现在我的代码真的很乱,但请暂时避免这个。我会尝试让它变得更好,但首先要解决这个问题。
答案 0 :(得分:3)
您正在获得类强制转换异常,因为在活动A中您将Long值作为id并且您试图在活动B中将其作为整数。更改此
int rest_id;
到
long rest_id;
和这个
rest_id = b.getInt("id");
到
rest_id = b.getLong("id");