使ListFragment打开一个活动

时间:2015-09-24 19:45:44

标签: android

Oke所以我对android studio和Android app dev一般都是新手,但我想制作一个我的青少年运动可以用作数据库的应用程序,但我遇到了问题。

所以我有一个包含所有组的抽屉,如果你按下抽屉按钮,它会打开一个带有名字的ListFragment,我想要发生的是当我按下名称时,活动显示的名称是一张照片和一个电话号码。但我似乎无法弄清楚如何从该listFragment中打开一个活动。

这是ListFragment.java

package app.jarifile.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
* Created by User on 23/09/2015.
*/
public class Givers_Fragment extends ListFragment {

Intent i;

String[] listitems = {
        "(Takleider)",
        "(Takleider)",
        "(Leider)",
        "(Leider)"
};
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listitems));
}

@Override
public void onListItemClick(ListView l, View v, int position, long id){

    switch(position){
        case 0:
            i = new Intent(getActivity(), Test_Activity.class);
            break;
        case 1:
            i = new Intent(getActivity(), Test_Activity.class);
            break;
        case 2:
            i = new Intent(getActivity(), Test_Activity.class);
            break;
    }
    startActivity(i);
}

这是Test_Activity代码。

package app.jarifile.test;

import android.app.Activity;
import android.os.Bundle;



/**
* Created by User on 24/09/2015.
*/

public class Test_Activity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);
}

这就是logcat所说的

09-28 18:48:28.915 24081-24081 / app.jarifile.test E / AndroidRuntime:FATAL EXCEPTION:main     处理:app.jarifile.test,PID:24081     android.content.ActivityNotFoundException:无法找到显式活动类{app.jarifile.test / app.jarifile.test.Test_Activity};你有没有在AndroidManifest.xml中声明这个活动?             在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1793)             在android.app.Instrumentation.execStartActivity(Instrumentation.java:1515)             在android.app.Activity.startActivityForResult(Activity.java:4026)             在android.app.Activity.startActivityForResult(Activity.java:3973)             在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)             在android.app.Activity.startActivity(Activity.java:4297)             在android.app.Activity.startActivity(Activity.java:4265)             在app.jarifile.test.Givers_Fragment.onListItemClick(Givers_Fragment.java:51)             在android.support.v4.app.ListFragment $ 2.onItemClick(ListFragment.java:58)             在android.widget.AdapterView.performItemClick(AdapterView.java:339)             在android.widget.AbsListView.performItemClick(AbsListView.java:1544)             在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3721)             在android.widget.AbsListView $ 3.run(AbsListView.java:5660)             在android.os.Handler.handleCallback(Handler.java:739)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:145)             在android.app.ActivityThread.main(ActivityThread.java:6837)             at java.lang.reflect.Method.invoke(Native Method)             在java.lang.reflect.Method.invoke(Method.java:372)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 09-28 18:48:31.635 24081-24081 / app.jarifile.test I / Process:发送信号。 PID:24081 SIG:9

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试用getActivity()替换startActivity(i).startActivity(i)

答案 1 :(得分:0)

您尚未在Test_Activity中声明AndroidManifest.xml。您的应用中的所有活动都必须在那里声明。以下是一个可能的示例:

 <manifest ... >
    <application ... >
      <activity android:name=".Test_Activity" />
      ...
    </application ... >
  ...
  </manifest>

http://developer.android.com/guide/components/activities.html#Declaring