我正在尝试通过新意图将所选项目发送到另一个活动。但是当我点击项目时,显示application has been stopped working
消息并终止应用程序。
这是我的第一个活动代码
public class AndroidListViewActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(AndroidListViewActivity.this, R.layout.list_item, R.id.label, adobe_products));
ListView lv =getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
// TODO Auto-generated method stub
String product = ((TextView) view).getText().toString();
Intent curentScreen = new Intent(getApplicationContext(), SingleItem.class);
curentScreen.putExtra("product", product);
startActivity(curentScreen);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_list_view, menu);
return true;
}
}
第二个活动代码。
public class SingleItem extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item_view);
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
}
}
stacktrace:
08-13 14:05:35.678: D/AndroidRuntime(267): Shutting down VM
08-13 14:05:35.698: W/dalvikvm(267): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-13 14:05:35.728: E/AndroidRuntime(267): FATAL EXCEPTION: main
08-13 14:05:35.728: E/AndroidRuntime(267): java.lang.ClassCastException: java.lang.String
08-13 14:05:35.728: E/AndroidRuntime(267): at com.list.androidlistviewactivity.AndroidListViewActivity$1.onItemClick(AndroidListViewActivity.java:39)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.widget.ListView.performItemClick(ListView.java:3382)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.os.Handler.handleCallback(Handler.java:587)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
08-13 14:05:35.728: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 14:05:35.728: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 14:05:35.728: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
08-13 14:05:35.728: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 14:05:35.728: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 14:05:35.728: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
替换此
String product = ((TextView) view).getText().toString();
与
TextView tv = (TextView)view.findViewById(R.id.label); //initialize text view withid label
String product = tv.getText().toString(); //get the text
或者
String product = (String)parent.getItemAtPosition(pos);
Toast.makeText(getApplicationContext(), product,Toast.LENGTH_SHORT).show();
然后
Intent curentScreen = new Intent(ActivityName.this, SingleItem.class);
curentScreen.putExtra("product", product);
startActivity(curentScreen);
示例:此处单击列表行时,您将看到吐司。
LIST_ITEM
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/label" // textview with id label
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="TextView" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
MainActivity
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] planets = getResources().getStringArray(R.array.planets_array);
this.setListAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, R.id.label, planets));
ListView lv =getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
// TODO Auto-generated method stub
TextView tv = (TextView)view.findViewById(R.id.label);
String product = tv.getText().toString();
Toast.makeText(getApplicationContext(), product,Toast.LENGTH_SHORT).show();
}
});
}
}
快照
答案 1 :(得分:1)
你开始第二次活动的方式不对。
替换您创建意图的行
Intent curentScreen = new Intent(getApplicationContext(), SingleItem.class);
与
Intent intent = new Intent(AndroidListViewActivity.this, SingleItem.class);
作为Intent构造函数中的第一个参数,您必须传递源Activity,即this
。由于您在OnItemClickListener中调用该函数,因此使用AndroidListViewActivity.this
调用它。希望这能解决错误。
答案 2 :(得分:0)
相反,您可以获得字符串product
,如下所示:
替换onItemClick
String product = ((TextView) view).getText().toString();
与
TextView tv = (TextView)view.findViewById(R.id.textview1);
String product = tv.getText().toString();