在 android 中,我创建了一个包含一些值(字符串)的表。单击表中的任何值时,应在另一个活动中显示相同的值。
我创建了 2 个活动。第一个活动包含表(使用表布局)和值(字符串值都是 textviews)。现在单击表中的任何值(即 textview),相同的值应该显示在第二个活动。我试过了,但我无法得到输出。这可能吗??
第一个活动:
public class Table_LayoutActivity extends Activity
{
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table=(TableLayout)findViewById(R.id.tableLayout1);
table.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String value=((TextView)v).getText().toString();
Intent intent=new Intent(getApplicationContext(),Selected_Item_Activity.class);
intent.putExtra("value", value);
startActivity(intent);
}
});
}
}
第二个活动:
public class Selected_Item_Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_item);
Intent intent=getIntent();
String value=intent.getStringExtra("value");
TextView text=(TextView)findViewById(R.id.tv);
text.setText(value);
}
}
java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.tablelayout/com.tablelayout.Selected_Item_Activity}:java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)android.app.ActivityThread.access$2300(ActivityThread.java:125)android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)android.app.ActivityThread.main(ActivityThread.java:467)java.lang.reflect.Method.invokeNative(Native Method)java.lang.reflect.Method.invoke(Method.java:521)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.
android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3592)android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3672)android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1395)android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1321)android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)android.view.LayoutInflater.rInflate(LayoutInflater.java:620)android.view.LayoutInflater.inflate(LayoutInflater.java:407)android.view.LayoutInflater.inflate(LayoutInflater.java:320)android.view.LayoutInflater.inflate(LayoutInflater.java:276)
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)android.app.Activity.setContentView(Activity.java:1647)
com.tablelayout.Selected_Item_Activity.onCreate(Selected_Item_Activity.java:12)android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)