我应该在清单中声明MainFragmentDemoActivity吗? [英] Should i declare the MainFragmentDemoActivity in manifest?

查看:43
本文介绍了我应该在清单中声明MainFragmentDemoActivity吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习片段.我单击一个片段类,我的应用程序崩溃了.我已经在清单中声明了它.但是为什么会发生呢.我的菜单类

I am trying to learn fragment.I am clicking a fragment class and my app crashes.I have declared it in the manifest..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();
    }
}

我的MainFragmentDemoActivity类

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

}

我的清单

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

我还有另外两个类ListFragmentDemo和DetailFragmentDemo.这些是我从MainFragmentDemoActivity.My xml实现的片段

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

我正在从此链接中学习

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

我的ListFragmentDemo

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

}

我的DetailFragmentDemo

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

推荐答案

您的 ListFragmentDemo 源自 android.support.v4.app.Fragment (片段类)而不是 android.app.Fragment (Android内的版本)

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)

更改

import android.support.v4.app.Fragment;

import android.app.Fragment;

ListFragmentDemo 类中的

in the ListFragmentDemo class

这篇关于我应该在清单中声明MainFragmentDemoActivity吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆