我有一个片段Activity Bhawan_page1,我想在按钮点击时调用另一个活动,但我收到两个错误 1.无法解决方法' findViewById(int)' 2.无法解析构造函数' Intent(匿名android.view.View.OnClickListener,java.lang.Class)'
以下是活动的代码
package in.ahmedraza.journey;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import in.ahmedraza.material_tab.ObservableScrollView;
import in.ahmedraza.material_tab.ScrollUtils;
import in.ahmedraza.material_tab.Scrollable;
public class Bhawan_page1 extends FlexibleSpaceWithImageBaseFragment<ObservableScrollView> {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bhawan_page1, container, false);
final ObservableScrollView scrollView = (ObservableScrollView) view.findViewById(R.id.scroll);
// TouchInterceptionViewGroup should be a parent view other than ViewPager.
// This is a workaround for the issue #117:
// https://github.com/ksoichiro/Android-ObservableScrollView/issues/117
scrollView.setTouchInterceptionViewGroup((ViewGroup) view.findViewById(R.id.fragment_root));
// Scroll to the specified offset after layout
Bundle args = getArguments();
if (args != null && args.containsKey(ARG_SCROLL_Y)) {
final int scrollY = args.getInt(ARG_SCROLL_Y, 0);
ScrollUtils.addOnGlobalLayoutListener(scrollView, new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, scrollY);
}
});
updateFlexibleSpace(scrollY, view);
} else {
updateFlexibleSpace(0, view);
}
scrollView.setScrollViewCallbacks(this);
return view;
Button btn1 = (Button) findViewById(R.id.full_story_bhawan);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(this, DialogMainActivity.class);
startActivity(myIntent);
}
});
}
@Override
protected void updateFlexibleSpace(int scrollY) {
// Sometimes scrollable.getCurrentScrollY() and the real scrollY has different values.
// As a workaround, we should call scrollVerticallyTo() to make sure that they match.
Scrollable s = getScrollable();
s.scrollVerticallyTo(scrollY);
// If scrollable.getCurrentScrollY() and the real scrollY has the same values,
// calling scrollVerticallyTo() won't invoke scroll (or onScrollChanged()), so we call it here.
// Calling this twice is not a problem as long as updateFlexibleSpace(int, View) has idempotence.
updateFlexibleSpace(scrollY, getView());
}
@Override
protected void updateFlexibleSpace(int scrollY, View view) {
ObservableScrollView scrollView = (ObservableScrollView) view.findViewById(R.id.scroll);
// Also pass this event to parent Activity
BhawanHome parentActivity =
(BhawanHome) getActivity();
if (parentActivity != null) {
parentActivity.onScrollChanged(scrollY, scrollView);
}
}
}
xml文件的代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<in.ahmedraza.material_tab.ObservableScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/flexible_space_image_height"
android:background="@android:color/transparent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/bhawan_story" />
<Button
android:id="@+id/full_story_bhawan"
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Complete Story"
android:onClick="true"
android:padding="@color/white"
android:background="@color/red"
android:gravity="center"/>
</LinearLayout>
</in.ahmedraza.material_tab.ObservableScrollView>
</FrameLayout>
答案 0 :(得分:2)
片段没有findViewById方法。需要从
修复此方法的单次调用Button btn1 = (Button) findViewById(R.id.full_story_bhawan);
到
Button btn1 = (Button) view.findViewById(R.id.full_story_bhawan);
第二件事。返回statemant后不要写代码。它永远不会被执行。
设置onCreateView
后,移动onClickListener
的返回值。
最后是onClickListener。 改变这个:
Intent myIntent = new Intent(this, DialogMainActivity.class);
到
Intent myIntent = new Intent(getActivity(), DialogMainActivity.class);
Intent构造函数的第一个参数是context。代码中的this
引用匿名onClickListener
而不是任何类型的上下文,因此需要修复它。
希望它能解决你的问题。
编辑:
android:padding="@color/white"
这永远无法奏效:P将其更改为正确的尺寸值。