2017-02-09 5 views
0

если вы можете мне помочь:Инициировать деятельность по внутреннему классу не статической

Я создал пользовательский класс для определения параметров ArrayList.

В этот класс я объявил Фрагменты и Контейнеры.

Я могу ', t использовать статический метод, чтобы объявить на манифесте, чтобы инициировать класс контейнера.

Но, если я использую «нестационарный», контейнер не открывается - возвращает ошибку «Невозможно создать экземпляр активности ... CustomClass $ Container не имеет конструктора нулевых аргументов».

Думаю, мне нужно инициировать активность (Container-Fragments) нестационарным внутренним классом. Но я не знаю, как это сделать.

Что я могу сделать? Tks за помощью.

Пользовательский класс:

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

Класс йоту 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> 

Ну, ТКС!

ответ

0

У вас просто очень странная установка здесь. Деятельность не должна быть внутренним классом. Он должен быть классом верхнего уровня и иметь любые другие классы, необходимые для отображения/выполнения Activity. У вас перевернута вся ваша иерархия. Ваши фрагменты также не должны быть подклассом. Я не знаю, где у вас такая настройка, но это просто неправильно.

+0

Это действительно не соответствует стандартам. Я попытался заставить его работать, но кажется, что это даже не возможно. Спасибо Gabe Sechan. –

Смежные вопросы