当我点击我的按钮" btn_search"在选择了项目" DEF"之后,我的应用程序崩溃了,我不知道如何解决这个问题。
感谢您的帮助,对不起我的英语,我是法国人。
我的代码(MenuDocumentation)
package ssip.ssip;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MenuDocumentation extends Fragment {
View rootview;
private Button btn_search;
ArrayAdapter adapterSpinner_1;
Spinner spin1;
String spn_1_Selected_item;
String[] spinn_marque = {"Veuillez choisir une marque","DEF"};
TextView txtdocumentation;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.activity_menu_documentation, container, false);
btn_search = (Button) rootview.findViewById(R.id.btn_search);
spin1 = (Spinner) rootview.findViewById(R.id.spin1);
txtdocumentation = (TextView) rootview.findViewById(R.id.txtdocumentation);
adapterSpinner_1 = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,spinn_marque);
spin1.setAdapter(adapterSpinner_1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spn_1_Selected_item = parent.getItemAtPosition(position).toString();
if (spn_1_Selected_item.equals("Veuillez choisir une marque"))
txtdocumentation.setText("");
if (spn_1_Selected_item.equals("DEF"))
btn_search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), DocumentationDef.class);
startActivity(intent);
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootview;
}
}
我的代码(DocumentationDef)
package ssip.ssip;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class DocumentationDef extends Fragment {
View rootview;
private Button btn_search;
ArrayAdapter adapterSpinner_1;
Spinner spin1;
String spn_1_Selected_item;
String[] spinn_element = {"Veuillez choisir un élément","RAPPORT ASSOCIATION"};
TextView tv1;
TextView tv2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.activity_documentation_def, container, false);
btn_search = (Button) rootview.findViewById(R.id.btn_search);
spin1 = (Spinner) rootview.findViewById(R.id.spin1);
tv1 = (TextView) rootview.findViewById(R.id.tv1);
tv2 = (TextView) rootview.findViewById(R.id.tv2);
adapterSpinner_1 = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,spinn_element);
spin1.setAdapter(adapterSpinner_1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spn_1_Selected_item = parent.getItemAtPosition(position).toString();
if (spn_1_Selected_item.equals("Veuillez choisir un élément"))
tv1.setText("");
tv2.setText("");
if (spn_1_Selected_item.equals("RAPPORT ASSOCIATION")){
tv1.setText("FORTE S");
tv2.setText("RAPPORT");
btn_search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://google.fr";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootview;
}
}
我的Logcat
09-25 20:07:28.965 23029-23029/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 0
09-25 20:07:29.045 23029-23029/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 1
09-25 20:07:29.045 23029-23029/ssip.ssip I/Timeline: Timeline: Activity_launch_request id:ssip.ssip time:78837586
09-25 20:07:29.075 23029-23029/ssip.ssip D/AndroidRuntime: Shutting down VM
09-25 20:07:29.085 23029-23029/ssip.ssip E/AndroidRuntime: FATAL EXCEPTION: main
Process: ssip.ssip, PID: 23029
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ssip.ssip/ssip.ssip.DocumentationDef}: java.lang.ClassCastException: ssip.ssip.DocumentationDef cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassCastException: ssip.ssip.DocumentationDef cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1095)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3084)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
解
if (spn_1_Selected_item.equals("DEF"))
btn_search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
DocumentationDef DocumentationDef = new DocumentationDef();
transaction.replace(R.id.content_frame, DocumentationDef);
transaction.commit();
}
});
答案 0 :(得分:2)
DocumentationDef2是一个片段,因为它扩展了一个片段。 你的代码 new Intent(getActivity(),DocumentationDef.class);不管用。 使用FragmentManager和FragmentTransactions将FolderDocumentation片段替换为DocumentationDef2。或者创建一个新的活动DocumentationDefActivity并将片段放在DocumentationDefActivity
中