我在Java文件中的TableRow中使用OnClick我正在使用OnClickListener,但是当我运行应用程序并尝试使用片段时,应用程序停止。
我想通过点击片段内的TableRow来实现这一点,该片段在新活动开放的片段内。
这是Java代码文件:
package com.hello.turidf;
import com.hello.turidf.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class TabsIndexM001Help extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.m_001_help, container, false);
Button openmap = (Button)rootView.findViewById(R.id.view_map);
openmap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View rootView) {
Intent intent = new Intent(getActivity(), M001MapActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
这是XML代码文件(片段):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
.......>
<TableLayout
.......>
<TabelRow
android:onClick="open_m001_map"
....>
........
</TableRow>
</TableLayout>
</ScrollBiew>
这是logcat:
12-22 18:53:38.862: E/dalvikvm-heap(2783): ooh, a big allocation 3240000.
12-22 18:53:42.926: W/dalvikvm(2783): VFY: unable to resolve direct method 11264: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
12-22 18:53:58.421: W/dalvikvm(2783): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-22 18:53:58.451: E/AndroidRuntime(2783): FATAL EXCEPTION: main
12-22 18:53:58.451: E/AndroidRuntime(2783): java.lang.ClassCastException: android.widget.ImageView
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.hello.turidf.TabsIndexM001Help.onCreateView(TabsIndexM001Help.java:21)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1766)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1901)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:848)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872)
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2196)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.os.Looper.loop(Looper.java:130)
12-22 18:53:58.451: E/AndroidRuntime(2783): at android.app.ActivityThread.main(ActivityThread.java:3717)
12-22 18:53:58.451: E/AndroidRuntime(2783): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:53:58.451: E/AndroidRuntime(2783): at java.lang.reflect.Method.invoke(Method.java:507)
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
12-22 18:53:58.451: E/AndroidRuntime(2783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
12-22 18:53:58.451: E/AndroidRuntime(2783): at dalvik.system.NativeStart.main(Native Method)
12-22 18:54:13.876: W/dalvikvm(2985): VFY: unable to resolve direct method 11264: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
答案 0 :(得分:0)
您正在尝试将ImageView
投射到Button
。
Button openmap = (Button)rootView.findViewById(R.id.view_map);
用
改变这一行ImageView openMap = (ImageView) rootView.findViewById(R.id.view_map);
再试一次。
答案 1 :(得分:0)
首先,请不要随时关注点按&#39;如果在java代码中使用SetOnClickListener,则在xml文件中输入属性。在你的情况下,它足以调用名为&#34; open_m001_map&#34;的函数。在你的java代码。
所以你有两个选择。 1.点击&#39; onClick&#39; xml代码中的属性并为其创建一个函数,如下所示。
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRowOne"
android:onClick="open_m001_map">
.....
</TableRow>
片段内部的java代码就像这样。
public void open_m001_map(){
Intent i=new Intent(getActivity(),secondActivity.class);
startActivity(i);
}
在xml文件中,
<TableRow
android:layout_width="match_parent"
adroid:layout_height="match_parent"
android:id="@+id/tableRowOne"
android:onClick="">
在java代码中,
TableRow tableRowOne=(TableRow)view.findViewById(R.id.tableRowOne);
tableRowOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(getActivity(),secondActivity.class);
startActivity(i);
}
});
&#39; secondActivity&#39;是您要打开的活动的名称。