gpt4 book ai didi

android - ListView 项目点击新 fragment Activity - "Unfortunately, App has stopped"

转载 作者:行者123 更新时间:2023-11-30 00:06:42 25 4
gpt4 key购买 nike

我正在为旅游景点制作一个 VR View,在我的应用程序中有两个选项卡 1)“欢迎 fragment ”和 2)“ttsd”。在 ttsd fragment 中,我放置了 listviewonclick。每次我选择 ttsd 调用的项目时,它总是崩溃(“不幸的是,Atdrag App 也已停止”)但后来我尝试更改并将其放入 Activity 它作品。下面我添加了我的代码。我认为我的代码在调用 fragment Activity 的地方出了问题。

MainActivity 选项卡布局:

package com.example.computer.vratdragfinal;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
assert tabLayout != null;
tabLayout.addTab(tabLayout.newTab().setText(R.string.welcome));
tabLayout.addTab(tabLayout.newTab().setText(R.string.venue));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new WelcomeFragment();
case 1:
return new ttsd();
}
return null;
}

@Override
public int getCount() {
return 2;
}
};
assert viewPager != null;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

public void refreshNow() {
finish();
overridePendingTransition(0,0);
startActivity(getIntent());
overridePendingTransition(0,0);

}

}

ttsd fragment Activity :

package com.example.computer.vratdragfinal;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Intent;
import android.widget.AdapterView;

public class ttsd extends Fragment {

public ttsd() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ttsd, container, false);
// Inflate the layout for this fragment
String[] awayStrings = {"Tubbataha Reef", "San Agustin Church", "Mayon Volcano", "Malapascua Island",
"Puerto Galera", "Puerto Princesa Undergound River", "Donsol", "Banaue Rice Terraces",};

ListView lv = (ListView) view.findViewById(R.id.listView2);

ArrayAdapter<String> lva = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, awayStrings);
lv.setAdapter(lva);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==0) {
Intent myintent = new Intent(view.getContext(), toptourist1.class);
startActivityForResult(myintent, 0);
}

if(position==1) {
Intent myintent = new Intent(view.getContext(), toptourist2.class);
startActivityForResult(myintent, 1);
}
}
});

final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.fragment_away);

mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
((MainActivity) getActivity()).refreshNow();
Toast.makeText(getContext(), "Refresh Layout working", Toast.LENGTH_LONG).show();

}
}
);

return view;
}

}

toptourist1 当我点击 ttsd 中的项目时 fragment Activity 崩溃:

package com.example.computer.vratdragfinal;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.vr.sdk.widgets.pano.VrPanoramaView;

public class toptourist1 extends Fragment {

private VrPanoramaView panoWidgetView;
private ImageLoaderTask backgroundImageLoaderTask;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_toptourist1, container,false);
panoWidgetView = (VrPanoramaView) v.findViewById(R.id.pano_view);
return v;
}

@Override
public void onPause() {
panoWidgetView.pauseRendering();
super.onPause();
}

@Override
public void onResume() {
panoWidgetView.resumeRendering();
super.onResume();
}

@Override
public void onDestroy() {

panoWidgetView.shutdown();
super.onDestroy();
}

private synchronized void loadPanoImage() {
ImageLoaderTask task = backgroundImageLoaderTask;
if (task != null && !task.isCancelled()) {
task.cancel(true);
}

VrPanoramaView.Options viewOptions = new VrPanoramaView.Options();
viewOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;

String panoImageName = "sample_converted.jpg";

task = new ImageLoaderTask(panoWidgetView, viewOptions, panoImageName);
task.execute(getActivity().getAssets());
backgroundImageLoaderTask = task;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadPanoImage();
}

}

LogCat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.computer.vratdragfinal, PID: 5850
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.computer.vratdragfinal/com.example.computer.vratdragfinal.toptourist1}: java.lang.ClassCastException: com.example.computer.vratdragfinal.toptourist1 cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: com.example.computer.vratdragfinal.toptourist1 cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Application terminated.

最佳答案

查看下面的 Logcat 行:

 Caused by: java.lang.ClassCastException: com.example.computer.vratdragfinal.toptourist1 cannot be cast to android.app.Activity

它告诉您 Android 无法将 toptourist1 类转换为 Activity,因为 Android 需要一个 Activity 类。

要启动一个Activity,您应该创建一个带有Activity 类的Intent。您正在使用 Fragment 类创建 Intent 。 startActivityForResult 方法用于启动 Activity 而不是 Fragment。

  Intent myintent = new Intent(view.getContext(), toptourist2.class);
startActivityForResult(myintent, 1);

这里 Intent 构造函数的第二个参数应该是一个 Activity 类。如果你想使用 Fragment,你必须使用 FragmentTransaction

检查 Fragment教程与检查this问答。

关于android - ListView 项目点击新 fragment Activity - "Unfortunately, App has stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48841409/

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