我是android应用程序构建的新手,这是我大学项目的要求。请帮帮我。
我的应用程序有滑动标签,使用碎片。我有5个选项卡和5个片段,在一些选项卡(片段)中,我需要导航到片段中的新片段;例如,当在Tab1中我有按钮时,按下按钮会将用户带到新的片段,我已经在互联网上搜索并设法编写了代码。我的问题是,当在Tab1中按下按钮进入新片段时,我会得到"应用程序停止工作",请帮助我
import android.content.Intent;
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.Button;
import android.view.View.OnClickListener;
public class Unsafe extends Fragment implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.unsafe, container, false);
Button button1 = (Button) v.findViewById(R.id.u1);
button1.setOnClickListener(this);
return v;
}
public void onClick(View v) {
startActivity(new Intent(getActivity(),unsafe1.class));
}
}
更新-发布Logcat
12-03 08:59:39.538 1460-1460/com.example.hfacs D/AndroidRuntime﹕ Shutting down VM
12-03 08:59:39.538 1460-1460/com.example.hfacs W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a77b90)
12-03 08:59:39.538 379-645/system_process I/ActivityManager﹕ START u0 {cmp=com.example.hfacs/.Unsafe1} from pid 1460
12-03 08:59:39.588 1460-1460/com.example.hfacs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.hfacs, PID: 1460
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3423)
at android.app.Activity.startActivityForResult(Activity.java:3384)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
at com.example.hfacs.Unsafe$1.onClick(Unsafe.java:28)
at android.view.View.performClick(View.java:4424)
at android.view.View$PerformClick.run(View.java:18383)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
我在logcat中发现了这一行:
ndroid.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?
这意味着您没有在AndroidManifest.xml
文件中声明活动Unsafe1
。
我认为您不应该在onClick中调用startActivity()。相反,当用户单击按钮时,通知包含此片段的FragmentActivity。在FragmentActivity中,调用ViewPager.setCurrentItem(position),其中position是int。然后视图寻呼机将滚动到所需的选项卡。
我不确定这是否是最好的方法。但我以前试过,效果很好。在FragmentActivity中添加一个功能,用于滚动到所需的选项卡。
public static void scroll(int pos){
mViewPager.setCurrentItem(pos); //mViewPager is the ViewPager you have in FragmentActivity
}
这是onClickListener
Button btn = (Button) v.findViewById(R.id.u1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//when button clicked, call the scroll function
MainActivity.scroll(position_of_unsafe1); //MainActivity is the FragmentActivity name
}
});