对于学习目的我正在制作一个应用程序..当我在屏幕上按任何按钮之前改变我的方向时...它成功地改变方向并显示我已添加到“layout-land”文件夹中的相应布局...
但是当我按下按钮显示下一个广告,然后切换方向..我希望活动重启但我的应用程序崩溃...这里我需要帮助..
调试器剂量不显示抛出任何异常的行
MainActivity Class
package com.example.prototype;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import com.example.prototype.fragments.FragmentOne;
public class MainActivity extends FragmentActivity {
int fragShowButtonPressed =0;
boolean FragOneDisplayed=false;
boolean FragTwoDisplayed=false;
public void fragment_switch(View v){
Button showfrag1 = (Button)findViewById(R.id.buttonShowFrag1);
Button showfrag2 = (Button)findViewById(R.id.buttonShowFrag2);
showfrag1.setVisibility(showfrag1.INVISIBLE);
showfrag2.setVisibility(showfrag2.INVISIBLE);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch(v.getId()){
case(R.id.buttonShowFrag1): ft.replace(R.id.frameLayout1, new FragmentOne("frag_1"), null); fragShowButtonPressed=1; FragOneDisplayed=true; FragTwoDisplayed=false; break;
case(R.id.buttonShowFrag2): ft.replace(R.id.frameLayout1, new FragmentOne("frag_2"), null); fragShowButtonPressed=1; FragOneDisplayed=false; FragTwoDisplayed=true; break;
}
ft.addToBackStack(null);
ft.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("create", "Entered Oncreate");
setContentView(R.layout.activity_main);
if(savedInstanceState != null){
if(savedInstanceState.getInt("frag_button_pressed_stat")==1)
{
Button showfrag1 = (Button)findViewById(R.id.buttonShowFrag1);
Button showfrag2 = (Button)findViewById(R.id.buttonShowFrag2);
showfrag1.setVisibility(showfrag1.INVISIBLE);
showfrag2.setVisibility(showfrag2.INVISIBLE);
FragOneDisplayed=savedInstanceState.getBoolean("frag_one_display_stat");
FragTwoDisplayed=savedInstanceState.getBoolean("frag_two_display_stat");
}
else{}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if(fragShowButtonPressed ==1)
{
outState.putInt("frag_button_pressed_stat", fragShowButtonPressed);
outState.putBoolean("frag_one_display_stat", FragOneDisplayed);
outState.putBoolean("frag_two_display_stat", FragTwoDisplayed);
}
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
FragmentOne Class
package com.example.prototype.fragments;
import com.example.prototype.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
int id;
Context context;
public FragmentOne(String frag_name){
if(frag_name=="frag_1")
{
id=R.layout.frag_1;
}
else if(frag_name == "frag_2"){
id=R.layout.frag_2;
}
else{}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = inflater.getContext();
View v=inflater.inflate(id, container, false);
return v;
}
}
请注意当我使用空构造函数创建不同的片段类并直接提供int XML资源以解决inflate()函数问题
Taimoor Ali,谢谢你
登录CAT
07-09 23:35:45.387: E/AndroidRuntime(948): FATAL EXCEPTION: main
07-09 23:35:45.387: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prototype/com.example.prototype.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.prototype.fragments.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3809)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.access$700(ActivityThread.java:139)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.os.Looper.loop(Looper.java:156)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.main(ActivityThread.java:4987)
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 23:35:45.387: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 23:35:45.387: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 23:35:45.387: E/AndroidRuntime(948): at dalvik.system.NativeStart.main(Native Method)
07-09 23:35:45.387: E/AndroidRuntime(948): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.prototype.fragments.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
07-09 23:35:45.387: E/AndroidRuntime(948): at com.example.prototype.MainActivity.onCreate(MainActivity.java:44)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.Activity.performCreate(Activity.java:4538)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-09 23:35:45.387: E/AndroidRuntime(948): ... 12 more
07-09 23:35:45.387: E/AndroidRuntime(948): Caused by: java.lang.InstantiationException: can't instantiate class com.example.prototype.fragments.FragmentOne; no empty constructor
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.Class.newInstanceImpl(Native Method)
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.Class.newInstance(Class.java:1319)
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
07-09 23:35:45.387: E/AndroidRuntime(948): ... 19 more
答案 0 :(得分:1)
您的错误表明您的片段没有空构造函数:
07-09 23:35:45.387:E / AndroidRuntime(948):引起:java.lang.InstantiationException:无法实例化类com.example.prototype.fragments.FragmentOne;没有空构造函数
您需要更改配置,因为您的片段正在重建。
您需要以不同的方式实例化片段中的数据,例如:
public static final GridFragment newInstance(String tabId)
{
GridFragment f = new GridFragment();
Bundle bdl = new Bundle(2);
bdl.putString(TAB_ID, tabId);
f.setArguments(bdl);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
String tabId = getArguments().getString(TAB_ID);
if (application.currentReport != null)
{
this.odTab = application.currentReport.getODTabByTabId(tabId);
}
else
{
startActivity(new Intent(getActivity(), LoginScrActivity.class));
}
super.onCreate(savedInstanceState);
}
那样构造函数仍然是空的,但是你传递了你需要的字符串,另请参阅这个问题:
答案 1 :(得分:1)
片段必须具有空构造函数(不带参数)或无(默认)。如果要传递参数,请使用参数创建Bundle
并使用setArguments
。
这通常使用静态创建方法完成:
class MyFragment extends Fragment {
public static MyFragment newInstance(String name) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putString("name", name);
f.setArguments(bundle);
return f;
}
...
}
但是在你的情况下只发送布局的id?