我的頁面是活動頁面,現在我想改變片段,但它墜毀,爲什麼我變成碎片,應用程序崩潰
造成的:java.lang.ClassCastException:com.mac。活動不能投射到android.app.Activity
我不知道哪部分代碼崩潰了。所以我把我的代碼放在下面。
代碼:
public class Activity extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity, container, false);
final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
final Button btn = (Button) view.findViewById(R.id.btn);
//RECEIVE
Intent i = getActivity().getIntent();
String name = i.getExtras().getString("NAME_KEY");
String desc = i.getExtras().getString("DESCRIPTION_KEY");
//BIND
nameTxt.setText(name);
descTxt.setText(desc);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
alertDialogBuilder.setTitle("Do you want to login?");
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNeutralButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getActivity(), FacebookLogin.class);
startActivity(i);
}
})
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
TextView toolbar_title = (TextView) getActivity().findViewById(R.id.toolbar_title);
toolbar_title.setText("DETAIL");
}
@Override
public void onStop() {
super.onStop();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
首先,我雖然應聲因爲忘記把決賽的TextView和按鈕,但我已經把現在還在崩潰。我正在使用android studio。希望有人幫助謝謝。
你能分享崩潰日誌嗎? –
確保你正在傳遞bundle到這個意圖,因爲你正試圖訪問NAME_KEY。把你的崩潰日誌放在這裏。 –
引起:java.lang.ClassCastException:com.mac.Activity不能轉換爲android.app.Activity –