错误消息:由以下原因引起:java.lang.NoSuchMethodException:onClickShare [class android.view.View]
Error Message: Caused by: java.lang.NoSuchMethodException: onClickShare [class android.view.View]
我在单击图像按钮时收到上述错误消息。我不确定这里有什么问题。我要做的就是将文本框中的文字分享出来。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<TextView
android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="normal|bold"
android:textSize="16dip"
android:padding="5dip"
android:text="Title"
android:textColor="#001a90" />
<TextView
android:id="@+id/txtDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dip"
android:padding="5dip"
android:text="Description"
android:textColor="#000000"
android:textIsSelectable="false" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/imageButton8"
android:layout_gravity="center_horizontal"
android:onClick="onClickShare" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/separator1"
android:visibility="visible"
android:background="@android:color/darker_gray"/>
</LinearLayout>
Java
public class rssitemview extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssitemview);
}
public void onClickShare(View view){
TextView txtDesc = (TextView) findViewById(R.id.txtDescription);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, txtDesc.getText());
intent.setType("text/plain");
try {
startActivity(Intent.createChooser(intent, "Share Text..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(rssitemview.this, "There are no means to share.", Toast.LENGTH_SHORT).show();
}
startActivity(intent);
}
}
解决方法是在 ListAdapter 的 getView class 中添加 OnClickListener。
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ImageButton whBtn = null;
ImageButton fbBtn = null;
ImageButton twBtn = null;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)rssfeedFragment.this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
whBtn = (ImageButton) view.findViewById(R.id.whBtn);
fbBtn = (ImageButton) view.findViewById(R.id.fbBtn);
twBtn = (ImageButton) view.findViewById(R.id.twBtn);
}
if (whBtn != null) {
whBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}
if (fbBtn != null) {
fbBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}
if (twBtn != null) {
twBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}
我在单击图像按钮时收到上述错误消息。我不确定这里有什么问题。我要做的就是将文本框中的文字分享出来。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<TextView
android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="normal|bold"
android:textSize="16dip"
android:padding="5dip"
android:text="Title"
android:textColor="#001a90" />
<TextView
android:id="@+id/txtDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dip"
android:padding="5dip"
android:text="Description"
android:textColor="#000000"
android:textIsSelectable="false" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/imageButton8"
android:layout_gravity="center_horizontal"
android:onClick="onClickShare" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/separator1"
android:visibility="visible"
android:background="@android:color/darker_gray"/>
</LinearLayout>
Java
public class rssitemview extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssitemview);
}
public void onClickShare(View view){
TextView txtDesc = (TextView) findViewById(R.id.txtDescription);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, txtDesc.getText());
intent.setType("text/plain");
try {
startActivity(Intent.createChooser(intent, "Share Text..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(rssitemview.this, "There are no means to share.", Toast.LENGTH_SHORT).show();
}
startActivity(intent);
}
}
解决方法是在 ListAdapter 的 getView class 中添加 OnClickListener。
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ImageButton whBtn = null;
ImageButton fbBtn = null;
ImageButton twBtn = null;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)rssfeedFragment.this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
whBtn = (ImageButton) view.findViewById(R.id.whBtn);
fbBtn = (ImageButton) view.findViewById(R.id.fbBtn);
twBtn = (ImageButton) view.findViewById(R.id.twBtn);
}
if (whBtn != null) {
whBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}
if (fbBtn != null) {
fbBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}
if (twBtn != null) {
twBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Code goes here
}
}
});
}