通过内部 class 非静态启动 activity
Initiate activity by inner class non static
如果你能帮助我:
我创建了一个自定义 Class 来定义 ArrayList 的参数。
在 class 中,我声明了 Fragments 和 Container。
我不能使用静态方法在清单上声明以启动容器 class。
但是,如果我使用 "non static",容器不会打开 - return 错误 "Unable to instantiate activity ... CustomClass$Container has no zero argument constructor"。
认为我需要通过非静态内部 class 启动 activity(容器 - 片段)。但是,我不知道怎么办。
我能做什么?
感谢您的帮助。
自定义class:
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);
}
});
}
}
和清单:
<?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>
嗯,谢谢!
你这里的设置真的很奇怪。 Activity 不应是内部 class。它应该是顶级 class,并且拥有 display/execute Activity 所需的任何其他 classes。您的整个层次结构都颠倒了。你的片段也不应该是任何东西的子class。我不知道你从哪里得到这个设置,但它就是错误的。
如果你能帮助我:
我创建了一个自定义 Class 来定义 ArrayList 的参数。
在 class 中,我声明了 Fragments 和 Container。
我不能使用静态方法在清单上声明以启动容器 class。
但是,如果我使用 "non static",容器不会打开 - return 错误 "Unable to instantiate activity ... CustomClass$Container has no zero argument constructor"。
认为我需要通过非静态内部 class 启动 activity(容器 - 片段)。但是,我不知道怎么办。
我能做什么? 感谢您的帮助。
自定义class:
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);
}
});
}
}
和清单:
<?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>
嗯,谢谢!
你这里的设置真的很奇怪。 Activity 不应是内部 class。它应该是顶级 class,并且拥有 display/execute Activity 所需的任何其他 classes。您的整个层次结构都颠倒了。你的片段也不应该是任何东西的子class。我不知道你从哪里得到这个设置,但它就是错误的。