片段正试图转向活动

时间:2014-07-15 06:14:35

标签: java android eclipse

我知道这个答案可能很简单,但我无法弄清楚。我正在修改项目的示例代码,并在尝试启动应用程序时不断获取ClassCastException。说ListActivityFragment无法强制转换为android.app.Activity。什么我不理解(这可能是简单的部分)是为什么当我的主要是片段活动时它试图将它投射到一个活动上。我知道代码很粗糙,仍然试图完成它但不能超越它。

FragmentActivity

public class MainActivity extends FragmentActivity {
private boolean isLargeScreen = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (findViewById(R.id.normal_screen_layout) != null) {
        isLargeScreen = false;
        ListActivityFragment listActivityFragment = new ListActivityFragment();
        listActivityFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.normal_screen_layout, listActivityFragment)
                .commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.addItem:
        ListActivityFragment newFragment = new ListActivityFragment();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.normal_screen_layout, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
    default:
        return super.onOptionsItemSelected(item);
    }
    return true;
}

}




}

ListActivity

public class ListActivityFragment extends ListFragment implements
    OnClickListener {
private ArrayList<String> items = new ArrayList<String>();
private ListView listView;
private View outerView;
private Button deleteButton;
private ListActivityListener listener;

public interface ListActivityListener {
    public void meetingAdded();
}

public ListActivityFragment() {

}

public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        listener = (ListActivityListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    outerView = inflater.inflate(R.layout.list_of_meetings, container,
            false);
    listView = (ListView) outerView.findViewById(R.id.listView1);
    deleteButton = (Button) outerView.findViewById(R.id.deleteButton);
    createList();
    return outerView;
}

public void createList() {
    items.clear();
    Iterator<Items> iterator = ItemCollection.Instance().iterator();
    while (iterator.hasNext()) {
        items.add(iterator.next().toString());
    }

    listView.setAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, items));
}

@Override
public void onClick(DialogInterface arg0, int arg1) {
    ListView listView = getListView();
    for (int index = 0; index < listView.getChildCount(); index++) {
        View viewGroup = listView.getChildAt(index);
        CheckBox checkBox = (CheckBox) viewGroup
                .findViewById(R.id.itemSelectCheckBox);
        if (checkBox.isChecked()) {
            TextView textView = (TextView) viewGroup
                    .findViewById(R.id.productTextView);
            int key = (Integer) textView.getTag();
            ItemCollection.Instance().delete(key);
        }
    }
    createList();
}

@Override
public void onStart() {
    super.onStart();
}

public void onListItemClick(ListView l, View v, int position, long id) {
    getListView().setItemChecked(position, true);
}
}

继承Logcat

07-15 01:59:50.710: E/AndroidRuntime(878): FATAL EXCEPTION: main
07-15 01:59:50.710: E/AndroidRuntime(878): Process: com.example.assignment5, PID: 878
07-15 01:59:50.710: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to        instantiate activity    ComponentInfo{com.example.assignment5/com.example.assignment5.ListActivityFragment}:    java.lang.ClassCastException: com.example.assignment5.ListActivityFragment cannot be cast to android.app.Activity
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.os.Looper.loop(Looper.java:136)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 01:59:50.710: E/AndroidRuntime(878):  at java.lang.reflect.Method.invokeNative(Native Method)
07-15 01:59:50.710: E/AndroidRuntime(878):  at java.lang.reflect.Method.invoke(Method.java:515)
07-15 01:59:50.710: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 01:59:50.710: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 01:59:50.710: E/AndroidRuntime(878):  at dalvik.system.NativeStart.main(Native Method)
07-15 01:59:50.710: E/AndroidRuntime(878): Caused by: java.lang.ClassCastException: com.example.assignment5.ListActivityFragment cannot be cast to android.app.Activity
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-15 01:59:50.710: E/AndroidRuntime(878):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-15 01:59:50.710: E/AndroidRuntime(878):  ... 11 more
07-15 01:59:51.160: E/NetdConnector(383): NDC Command {65 bandwidth setiquota eth0 9223372036854775807} took too long (1117ms)

1 个答案:

答案 0 :(得分:0)

  

引起:java.lang.ClassCastException:   com.example.assignment5.ListActivityFragment无法强制转换为   android.app.Activity 07-15 01:59:50.710:E / AndroidRuntime(878):

正如错误所说 -

ListActivityFragment不是活动。所以你试图像活动那样访问它。

确保您没有在清单中声明ListActivityFragment。在你的清单中,如果你为片段创建了一个条目,那就错了。

还要确保您没有使用startActivity片段。因为这是为活动类而不是片段类完成的。