我想在我的片段中实现onclick以更改片段。
这是这个功能:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
super.onViewCreated(view, savedInstanceState);
imageView9 = (ImageView)view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
点击'因为我听到典型的按钮声音。但是我在点击时收到此错误:
ssl=0xaf840c00 cert_verify_callback x509_store_ctx=0xa17d2280 arg=0x0
ssl=0xaf840c00 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
这是我的main_activity布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarInner"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="@+id/content_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#212"
android:layout_below="@+id/toolbar_app_bar_layout">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/toolbar_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/top_points_bar" />
</LinearLayout>
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
app:tabMode="scrollable"
app:paddingStart="16dp"
app:tabPaddingStart="16dp"
app:tabPaddingEnd="16dp"
app:tabMinWidth="96dp"
app:tabGravity="center" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<android.support.design.widget.NavigationView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@color/md_brown_100"
app:menu="@menu/menu_drawer"
android:textColor="@color/okurwa"
app:theme="@style/MyTabStyle"
app:headerLayout="@layout/nav_drawer_header"
app:itemTextAppearance="@style/MyTabTextStyle"
android:clipToPadding="false">
<TextView
android:id="@+id/nav_drawer_wallet_id"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/nav_drawer_total_credits"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="200dp"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/nav_drawer_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="22sp"
android:layout_marginTop="150dp"/>
<!--<FrameLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom"-->
<!--android:background="@color/md_white_1000"-->
<!--android:elevation="4dp"-->
<!--android:layout_marginBottom="-96dp">-->
<!--<Button android:id="@+id/navigation_button_footer"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:text=""-->
<!--android:textSize="13sp"-->
<!--android:textColor="@color/md_grey_800"-->
<!--android:lines="3"-->
<!--android:gravity="center"-->
<!--style="@style/Widget.AppCompat.ActionButton"-->
<!--android:paddingTop="20dp"-->
<!--android:paddingLeft="20dp"-->
<!--android:paddingRight="20dp"-->
<!--android:paddingBottom="20dp"/>-->
<!--</FrameLayout>-->
</android.support.design.widget.NavigationView>
<RelativeLayout
android:id="@+id/lay_connection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/actmain_margintop"
android:background="#3c3c3c"
android:visibility="gone" >
<TextView
android:id="@+id/text_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/error_no_internet"
android:textColor="@color/md_white_1000"
android:textSize="@dimen/twentyfive" />
<RelativeLayout
android:id="@+id/lay_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:text="@string/error_no_internet"
android:textColor="@color/md_white_1000"
android:textSize="@dimen/twentyfive" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
请帮帮我们!我第一次看到这个错误。我不知道为什么我会收到此错误以及为什么它无法正常工作。感谢Stack&#39; s社区!
这是此片段的完整代码:
/**
* Created by otsma on 12.12.2016.
*/
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.commonutility.PreferenceConnector;
import com.justfashion.R;
import java.util.Random;
public class OneFragment extends Fragment {
final Random rnd = new Random();
public OneFragment() {
// Required empty public constructor
}
private static TextView creditWallet;
private String[] myString;
private static final Random rgenerator = new Random();
private TextView firstName, textView13,textView14,textView15,textView16,textView17,textView18;
private Animation animShake, wbijam, load1, load2, load3, load4, load5, load6, wbijam1, wbijam2, wbijam3, wbijam4, wbijam5;
private ImageView img, imageView9,imageView19,imageView4,imageView5,imageView6,imageView7,imageView8;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void onUpdateView(Context aiContext) {
// TODO Auto-generated method stub
if (aiContext != null && creditWallet != null)
creditWallet.setText(PreferenceConnector.readInteger(aiContext, PreferenceConnector.WALLETPOINTS, 0) + "");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
imageView9 = (ImageView) view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
img = (ImageView)view.findViewById(R.id.imgRandom);
imageView4 = (ImageView)view.findViewById(R.id.imageView4);
imageView5 = (ImageView)view.findViewById(R.id.imageView5);
imageView6 = (ImageView)view.findViewById(R.id.imageView6);
imageView7 = (ImageView)view.findViewById(R.id.imageView7);
imageView8 = (ImageView)view.findViewById(R.id.imageView8);
firstName = (TextView)view.findViewById(R.id.imie);
textView13 = (TextView)view.findViewById(R.id.textView13);
textView14 = (TextView)view.findViewById(R.id.textView15);
textView15 = (TextView)view.findViewById(R.id.textView16);
textView16 = (TextView)view.findViewById(R.id.textView17);
textView17 = (TextView)view.findViewById(R.id.textView18);
textView18 = (TextView)view.findViewById(R.id.textView19);
final Animation wbijam = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView13.startAnimation(wbijam);
final Animation wbijam1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView14.startAnimation(wbijam1);
final Animation wbijam2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView15.startAnimation(wbijam2);
final Animation wbijam3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView16.startAnimation(wbijam3);
final Animation wbijam4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView17.startAnimation(wbijam4);
final Animation wbijam5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView18.startAnimation(wbijam5);
final Animation animShake = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.shak);
img.startAnimation(animShake);
final Animation load2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load2);
imageView4.startAnimation(load2);
final Animation load3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load3);
imageView5.startAnimation(load3);
final Animation load4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load4);
imageView6.startAnimation(load4);
final Animation load5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load5);
imageView7.startAnimation(load5);
final Animation load6 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load6);
imageView8.startAnimation(load6);
firstName.setText("Hey,"+" " +PreferenceConnector.readString(getActivity().getApplicationContext(), PreferenceConnector.FIRST_NAME, ""));
// Inflate the layout for this fragment
final ImageView img = (ImageView) view.findViewById(R.id.imgRandom);
// return view;
// ^^^^ error remove it
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(q);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(9);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getActivity().getApplicationContext())
));
return view;
// ^^^ move it here
}
// I have 3 images named img_0 to img_2, so...
protected final static int getResourceID
(final String resName, final String resType, final Context ctx) {
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0) {
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
} else {
return ResourceID;
}
}
}
答案 0 :(得分:2)
删除super.onViewCreated(view,savedInstanceState);
回复你的观点
答案 1 :(得分:2)
请返回视图
retun view;
如果问题仍然存在
要确定问题是否与Onclick有关,请发表评论 onclick方法中的所有内容,并在其中放入Toast。所以我 我想我们可以清楚地了解这个问题..
答案 2 :(得分:1)
试试这个!!
替换以下行:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
到此:
android.app.FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
也不要忘记return view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
super.onViewCreated(view, savedInstanceState);
imageView9 = (ImageView)view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return view;
}
你的片段和片段管理器应该是同一类型。所以要么导入
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
OR
import android.app.Fragment;
import android.app.FragmentTransaction;
java文件
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
imageView9 = (ImageView) view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, newFragment)
.addToBackStack(null)
.commit();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
// your other contents.....
return view;
}
答案 3 :(得分:0)
您最好为这样的点击事件实现界面
public class fragment_test extends Fragment implements View.OnClickListener {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getItemId()) {
case R.id.action_zoom_in:
// .....
break;
}
return true;
}
}
}