如果我宣布在menifest的MainFragmentDemoActivity?menifest、MainFragmentDemoActivity

2023-09-03 22:30:58 作者:哥是老中医专治吹牛逼

我想学习fragment.I我点击的片段类和我的应用程序crashes.I已经在menifest..But宣布它为什么是happening..My Menu类

 公共类菜单扩展ListActivity {

串类[] = {SpinnerDemo,GridDemo,AutoCompleteDemo,DynamicDemo,
        WebViewDemo1,WebViewDemo3,LaunchDemo,LifecycleLoggingActivity,IntentCheckActivity,CallIntentActivity,
        MainFragmentDemoActivity,Simplebrowser,翻转,共享preFS,Internaldata
        Externaldata,Sqliteexample,GLexample,TextVoice,
        状态栏,SeekBarVolume};

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setListAdapter(新ArrayAdapter<字符串>(菜单。该,
            android.R.layout.simple_list_item_1,班));
}

@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    // TODO自动生成方法存根
    super.onListItemClick(L,V,位置ID);
    类ourclass;
    字符串路径=类[位置]
    尝试 {
        ourclass =的Class.forName(。com.example.practise+路径);
        意图ourintent =新的意图(菜单。该,ourclass);
        startActivity(ourintent);
    }赶上(ClassNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}
 

我的MainFragmentDemoActivity类

 公共类MainFragmentDemoActivity扩展活动实现ListFragmentDemo.Communicator {
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main_fragmenttesting);
}

@覆盖
公共无效消息(字符串OS_NAME){
    DetailFragmentDemo细节=(DetailFragmentDemo)getFragmentManager()findFragmentById(R.id.detail_Fragment)。
    如果(详细= NULL和放大器;!&安培; detail.isInLayout()){
        detail.setText(OS_NAME);
    }

}
 

我menifest

 <活动
        机器人:名称=com.example.practise.MainFragmentDemoActivity
        机器人:标签=MainFragmentDemoActivity>
    < /活性GT;
 

我有另外两个类ListFragmentDemo和DetailFragmentDemo.These是,我是从MainFragmentDemoActivity.My XML

implemening片段

 <片段
    机器人:ID =@ + ID / list_Fragment
    机器人:layout_width =0dp
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =1
    类=com.example.practise.ListFragmentDemo>&所述; /片断>

<片段
    机器人:ID =@ + ID / detail_Fragment
    机器人:layout_width =0dp
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =2
    类=com.example.practise.DetailFragmentDemo>

&所述; /片断>
 

logcat的

 了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.practise / com.example.practise.MainFragmentDemoActivity}
致:android.view.InflateException:二进制XML文件中的行#8:错误充气类片段
致:java.lang.ClassCastException:com.example.practise.ListFragmentDemo不能转换为android.app.Fragment
 

我从这个链接学习

的http://www.tutorialsbuzz.com/2014/03/android-fragments-example-ui-multi-pane.html

我ListFragmentDemo

 包com.example.practise;
进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.Button;
进口静态android.widget.AdapterView *。
公共类ListFragmentDemo扩展片段实现View.OnClickListener {
私人通讯沟通;
按钮android_btn,ios_btn,window_btn;
@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    如果(活动的instanceof通讯){
        沟通=(通讯)的活动;
    } 其他 {
        抛出新ClassCastException异常(activity.toString()+必须实现ListFragmentDemo);
    }
 }

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.list_fragment,集装箱,假);
    android_btn =(按钮)view.findViewById(R.id.android_btn_id);
    ios_btn =(按钮)view.findViewById(R.id.ios_btn_id);
    window_btn =(按钮)view.findViewById(R.id.windows_btn_id);
    android_btn.setOnClickListener(本);
    ios_btn.setOnClickListener(本);
    window_btn.setOnClickListener(本);
    返回查看;
}

公共接口通讯{
    公共无效消息(字符串OS_NAME);
}

@覆盖
公共无效的onClick(视图查看){
    开关(view.getId()){
        案例R.id.android_btn_id:
            updateFragment(机器人);
            打破;
        案例R.id.windows_btn_id:
            updateFragment(视窗);
            打破;
        案例R.id.ios_btn_id:
            updateFragment(IOS);
            打破;
    }
}
私人无效updateFragment(字符串OS_NAME){
    communicator.Message(OS_NAME);
}
 

}

我DetailFragmentDemo

 包com.example.practise;

 进口android.os.Bundle;
 进口android.app.Fragment;
 进口android.view.LayoutInflater;
 进口android.view.View;
 进口android.view.ViewGroup;
 进口android.widget.TextView;
 公共类DetailFragmentDemo扩展片段{
 @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.detail_fragment,集装箱,假);
    返回查看;
}
//我们调用这个方法是单击从listfragment按钮时
公共无效的setText(字符串项)
{
    TextView中的TextView =(TextView中)getView()findViewById(R.id.display_tv)。
    textView.setText(项目);
}
}
 

解决方案

ListFragmentDemo android.support.v4.app.Fragment (片断类的支持版本),而不是 android.app.Fragment (这里面的Andr​​oid版本)

修改

 进口android.support.v4.app.Fragment;
 

 进口android.app.Fragment;
 

ListFragmentDemo

I am trying to learn fragment.I am clicking a fragment class and my app crashes.I have declared it in the menifest..But why it is happening..My Menu class

public class Menu extends ListActivity {

String classes[] = { "SpinnerDemo", "GridDemo", "AutoCompleteDemo", "DynamicDemo",
        "WebViewDemo1", "WebViewDemo3", "LaunchDemo", "LifecycleLoggingActivity", "IntentCheckActivity", "CallIntentActivity",
        "MainFragmentDemoActivity", "Simplebrowser", "Flipper", "SharedPrefs", "Internaldata",
        "Externaldata", "Sqliteexample", "GLexample", "TextVoice",
        "StatusBar", "SeekBarVolume" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setListAdapter(new ArrayAdapter<String>(Menu.this,
            android.R.layout.simple_list_item_1, classes));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Class ourclass;
    String path = classes[position];
    try {
        ourclass = Class.forName("com.example.practise." + path);
        Intent ourintent = new Intent(Menu.this, ourclass);
        startActivity(ourintent);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

My MainFragmentDemoActivity class

public class MainFragmentDemoActivity extends Activity implements ListFragmentDemo.Communicator {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_fragmenttesting);
}

@Override
public void Message(String os_name) {
    DetailFragmentDemo detail=(DetailFragmentDemo)getFragmentManager().findFragmentById(R.id.detail_Fragment);
    if (detail != null && detail.isInLayout()) {
        detail.setText(os_name);
    }

}

My menifest

   <activity
        android:name="com.example.practise.MainFragmentDemoActivity"
        android:label="MainFragmentDemoActivity" >
    </activity>

I have two other class ListFragmentDemo and DetailFragmentDemo.These are the fragments which i am implemening from MainFragmentDemoActivity.My xml

 <fragment
    android:id="@+id/list_Fragment"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    class="com.example.practise.ListFragmentDemo"></fragment>

<fragment
    android:id="@+id/detail_Fragment"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    class="com.example.practise.DetailFragmentDemo">

</fragment>

logcat

   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.practise/com.example.practise.MainFragmentDemoActivity}
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
Caused by: java.lang.ClassCastException: com.example.practise.ListFragmentDemo cannot be cast to android.app.Fragment

I am learning from this link

http://www.tutorialsbuzz.com/2014/03/android-fragments-example-ui-multi-pane.html

my ListFragmentDemo

package com.example.practise;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import static android.widget.AdapterView.*;
public class ListFragmentDemo extends Fragment implements View.OnClickListener {
private Communicator communicator;
Button android_btn, ios_btn, window_btn;
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof Communicator) {
        communicator = (Communicator) activity;
    } else {
        throw new ClassCastException(activity.toString() + "must implement ListFragmentDemo");
    }
 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_fragment, container, false);
    android_btn = (Button) view.findViewById(R.id.android_btn_id);
    ios_btn = (Button) view.findViewById(R.id.ios_btn_id);
    window_btn = (Button) view.findViewById(R.id.windows_btn_id);
    android_btn.setOnClickListener(this);
    ios_btn.setOnClickListener(this);
    window_btn.setOnClickListener(this);
    return view;
}

public interface Communicator {
    public void Message(String os_name);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.android_btn_id:
            updateFragment("Android");
            break;
        case R.id.windows_btn_id:
            updateFragment("Windows");
            break;
        case R.id.ios_btn_id:
            updateFragment("IOS");
            break;
    }
}
private void updateFragment(String os_name){
    communicator.Message(os_name);
}

}

My DetailFragmentDemo

 package com.example.practise;

 import android.os.Bundle;
 import android.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 public class DetailFragmentDemo extends Fragment{
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {
    View view=inflater.inflate(R.layout.detail_fragment,container,false);
    return view;
}
// we call this method when button from listfragment is clicked
public void setText(String item)
{
    TextView textView=(TextView)getView().findViewById(R.id.display_tv);
    textView.setText(item);
}
}

解决方案

Your ListFragmentDemo extends from android.support.v4.app.Fragment (the support version of the Fragment class) and not android.app.Fragment (the version which is inside Android)

Change

import android.support.v4.app.Fragment;

to

import android.app.Fragment;

in the ListFragmentDemo class

相关推荐