我尝试了两种类型的代码来实现这一点,当我按下按钮进入另一个Activity时,它会一直强制关闭。我正在使用Fragment并且片段代码中有一个按钮,但我似乎无法让它工作。
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;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.androidbelieve.activity.LoginActivity;
import com.androidbelieve.helper.SQLiteHandler;
import com.androidbelieve.helper.SessionManager;
import java.util.HashMap;
public class PrimaryFragment extends Fragment {
private TextView txtName;
private Button btnLogout;
private SQLiteHandler db;
private SessionManager session;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.primary_layout, container, false);
txtName = (TextView) rootView.findViewById(R.id.username);
btnLogout = (Button) rootView.findViewById(R.id.logout);
// SqLite database handler
db = new SQLiteHandler(getActivity().getApplicationContext());
// session manager
session = new SessionManager(getActivity().getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
// Displaying the user details on the screen
txtName.setText(name);
// Logout button click event
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutUser();
}
});
Button button = (Button) rootView.findViewById(R.id.saleentry);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return rootView;
}
public void updateDetail() {
Intent intent = new Intent(getActivity(), SentFragment.class);
startActivity(intent);
}
我试图使用按钮转到另一个页面,但它总是失败 这是我的stacktrace ..这是我使用onclicklistener时的结果
04-07 22:42:37.285 27777-27777 / com.androidbelieve.drawerwithswipetabs&gt; E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.androidbelieve.drawerwithswipetabs,PID:27777 主题:主题:{default = overlay:system,iconPack:system,fontPkg:system,&gt; com.android.systemui = overlay:system} android.content.ActivityNotFoundException:无法找到明确的&gt;活动类&gt; {com.androidbelieve.drawerwithswipetabs / com.androidbelieve.drawerwithswipet&gt; abs.SentFragment};你在&gt; AndroidManifest.xml中声明了这个活动吗? 在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801) 在android.app.Instrumentation.execStartActivity(Instrumentation.java:1514) 在android.app.Activity.startActivityForResult(Activity.java:3930) 在android.app.Activity.startActivityForResult(Activity.java:3890) 在android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849) 在android.support.v4.app.FragmentActivity $ HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907) 在android.support.v4.app.Fragment.startActivity(Fragment.java:916) 在com.androidbelieve.drawerwithswipetabs.PrimaryFragment.updateDetail(PrimaryFragment.java:75) 在com.androidbelieve.drawerwithswipetabs.PrimaryFragment $ 2.onClick(PrimaryFragment.java:67) 在android.view.View.performClick(View.java:5204) 在android.view.View $ PerformClick.run(View.java:21156) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5456) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
首先,您必须在xml布局中添加框架布局。
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
然后点击
使用此代码 Button button = (Button) rootView.findViewById(R.id.saleentry);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new SentFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.remove(new PrimaryFragment ());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});