gpt4 book ai didi

java - 项目点击 fragment 导致的 Activity 崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 09:41:26 26 4
gpt4 key购买 nike

当我在选择项目“DEF”后单击“btn_search”按钮时,我的应用程序崩溃了,我不知道如何解决这个问题。

感谢您的帮助,抱歉我的英语不好,我是法国人。

我的代码(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;
}
}

我的日志

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();
}
});

最佳答案

DocumentationDef2 是一个 fragment ,因为它扩展了一个 fragment 。你的代码新的 Intent (getActivity(),DocumentationDef.class);不管用。使用 FragmentManager 和 FragmentTransactions 将 MenuDocumentation fragment 替换为 DocumentationDef2。或者创建一个新的 Activity DocumentationDefActivity 并将 fragment 放在 DocumentationDefActivity

关于java - 项目点击 fragment 导致的 Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39690118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com