如果你可以帮助我:
我创建了一个Custom Class来定义ArrayList的参数。
进入该类,我声明了Fragments和Container。
我可以使用静态方法在Manifest上声明启动Container类。
但是,如果我使用"非静态",容器不会打开 - 返回错误"无法实例化活动... CustomClass $ Container没有零参数构造函数&#34 ;
认为我需要通过非静态内部类启动活动(Container -Fragments)。但是,我不知道如何。
我该怎么办? 请求帮忙。
自定义类:
package com.example.android.guiaturistico;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CustomClass {
private int mImage;
private String mName;
private String mLocalization;
private int mFragmentI;
private int mFragmentII;
private int mFragmentIII;
public CustomClass (int image, String name, String localization, int fragmentI, int fragmentII, int fragmentIII){
mImage = image;
mName = name;
mLocalization = localization;
mFragmentI = fragmentI;
mFragmentII = fragmentII;
mFragmentIII = fragmentIII;
}
public int getImage() {
return mImage;
}
public void setImage(int mImage) {
this.mImage = mImage;
}
public String getName() {
return mName;
}
public void setName(String mName) {
this.mName = mName;
}
public String getLocalization() {
return mLocalization;
}
public void setLocalization(String mLocalization) {
this.mLocalization = mLocalization;
}
public int getFragmentI() {
return mFragmentI;
}
public void setFragmentI(int mFragmentI) {
this.mFragmentI = mFragmentI;
}
public int getFragmentII() {
return mFragmentII;
}
public void setFragmentII(int mFragmentII) {
this.mFragmentII = mFragmentII;
}
public int getFragmentIII() {
return mFragmentIII;
}
public void setFragmentIII(int mFragmentIII) {
this.mFragmentIII = mFragmentIII;
}
@Override
public String toString() {
return "Nome do local: " + mName + ".\n" +
"Endereço do local: " + mLocalization + ".";
}
@SuppressLint("ValidFragment")
public class FragmentInflaterI extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getFragmentI(), container, false);
}
}
@SuppressLint("ValidFragment")
public class FragmentInflaterII extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getFragmentII(), container, false);
}
}
@SuppressLint("ValidFragment")
public class FragmentInflaterIII extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getFragmentIII(), container, false);
}
}
public class Fragments extends FragmentPagerAdapter {
public Fragments (android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new FragmentInflaterI();
} else if (position == 1){
return new FragmentInflaterII();
} else {
return new FragmentInflaterIII();
}
}
@Override
public int getCount() {
return 3;
}
}
public class Container extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_container);
ViewPager viewPager = (ViewPager) findViewById(R.id.layout_container);
CustomClass.Fragments adapter = new CustomClass.Fragments(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
}
}
Class whit ArrayList和方法调用者onItemClick:
package com.example.android.guiaturistico;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class Hoteis extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hoteis);
final ArrayList <CustomClass> lista = new ArrayList<>();
CustomClass hoteis = new CustomClass(0, "", "", 0, 0, 0);
hoteis.setImage(R.mipmap.ic_hotel_white_48dp);
hoteis.setName(getString(R.string.name_hotel));
hoteis.setLocalization(getString(R.string.local_hotel));
hoteis.setFragmentI(R.layout.fragment_hotel1_perfil);
hoteis.setFragmentII(R.layout.fragment_hotel1_preco);
hoteis.setFragmentIII(R.layout.fragment_hotel1_contato);
lista.add(hoteis);
CustomClass hoteis2 = new CustomClass(0, "", "", 0, 0, 0);
hoteis2.setImage(R.mipmap.ic_hotel_white_48dp);
hoteis2.setName(getString(R.string.name_hotel2));
hoteis2.setLocalization(getString(R.string.local_hotel2));
hoteis2.setFragmentI(R.layout.fragment_hotel2_perfil);
hoteis2.setFragmentII(R.layout.fragment_hotel2_preco);
hoteis2.setFragmentIII(R.layout.fragment_hotel2_contato);
lista.add(hoteis2);
CustomClassAdapter itemAdapter = new CustomClassAdapter(this, lista);
ListView listView = (ListView)findViewById(R.id.lista_hoteis);
listView.setAdapter(itemAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Intent openFragment = new Intent(getApplicationContext(), CustomClass.Container.class);
startActivity(openFragment);
}
});
}
}
和Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.guiaturistico">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Hoteis"
android:label="@string/app_name_hoteis"
android:parentActivityName=".MainActivity"
/>
<activity android:name=".CustomClass$Container"
android:label="@string/app_name"
android:parentActivityName=".CustomClass$Container"
/>
</application>
</manifest>
好吧,tks!
答案 0 :(得分:0)
你这里有一个非常奇怪的设置。活动不应该是内部阶级。它应该是顶级类,并拥有显示/执行Activity所需的任何其他类。您将整个层次结构颠倒了。你的碎片不应该是任何东西的子类。我不知道你在哪里得到这个设置,但它错了。