gpt4 book ai didi

android - 空对象引用上的 FragmentActivity.getSupportFragmentManager()'

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:19 26 4
gpt4 key购买 nike

每次我尝试单击我的 ListView 以转到我的下一个 Activity 时,我都会收到一个空对象引用。它应该在第 31 行附近。所以这一行“adapter = new ViewPagerAdapter(myContext.getSupportFragmentManager(),TitlesAuto,Numboftabs);”

package android.evers.mickey.final_eindwerk_android;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

/**
* Created by Mickey on 22/01/16.
*/
public class AutoSelected extends Activity {
ViewPagerAdapter adapter;
ViewPager pager;
SlidingTabLayout tabs;
CharSequence TitlesAuto[]={"Info","Picture", "Sound"};
int Numboftabs = 3;
public FragmentActivity myContext;


@Nullable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_main_selected);

// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(myContext.getSupportFragmentManager(),TitlesAuto,Numboftabs);
pager = (ViewPager) myContext.findViewById(R.id.pagerAuto);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) myContext.findViewById(R.id.pagerAuto);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View

tabs = (SlidingTabLayout) myContext.findViewById(R.id.tabsAuto);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorAccent);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}}

这是我的错误:

08-14 01:27:49.230 23389-23389/? I/art: Not late-enabling -Xcheck:jni (already on)
08-14 01:27:49.273 23389-23389/? W/System: ClassLoader referenced unknown path: /data/app/android.evers.mickey.final_eindwerk_android-2/lib/x86
08-14 01:27:49.280 23389-23396/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
08-14 01:27:49.280 23389-23396/? I/art: Debugger is no longer active
08-14 01:27:49.384 23389-23403/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-14 01:27:49.467 23389-23403/? I/OpenGLRenderer: Initialized EGL, version 1.4
08-14 01:27:49.483 23389-23403/? W/EGL_emulation: eglSurfaceAttrib not implemented
08-14 01:27:49.483 23389-23403/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf3f100, error=EGL_SUCCESS
08-14 01:27:54.069 23389-23389/android.evers.mickey.final_eindwerk_android W/PathParser: Points are too far apart 4.000000596046461
08-14 01:27:55.273 23389-23389/android.evers.mickey.final_eindwerk_android W/PathParser: Points are too far apart 4.000000596046461
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: {"id":"1","naam":"test","info":"dit is een test","merk":"porshe","bouwjaar":"0000-00-00","foto":"http:\/\/dtsl.ehb.be\/~mickey.evers\/android\/images\/auto1.png"}
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: http://dtsl.ehb.be/~mickey.evers/android/images/auto1.png
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: test
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: {"id":"2","naam":"testfxfjj","info":"test2","merk":"testen","bouwjaar":"0000-00-00","foto":"http:\/\/dtsl.ehb.be\/~mickey.evers\/android\/images\/auto1.png"}
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: http://dtsl.ehb.be/~mickey.evers/android/images/auto1.png
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: testfxfjj
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: [test, testfxfjj]
08-14 01:27:56.205 23389-23389/android.evers.mickey.final_eindwerk_android D/AndroidRuntime: Shutting down VM
08-14 01:27:56.205 23389-23389/android.evers.mickey.final_eindwerk_android E/AndroidRuntime: FATAL EXCEPTION: main
Process: android.evers.mickey.final_eindwerk_android, PID: 23389
java.lang.RuntimeException: Unable to start activity ComponentInfo{android.evers.mickey.final_eindwerk_android/android.evers.mickey.final_eindwerk_android.AutoSelected}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
at android.evers.mickey.final_eindwerk_android.AutoSelected.onCreate(AutoSelected.java:31)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-14 01:32:56.222 23389-23389/? I/Process: Sending signal. PID: 23389 SIG: 9

最佳答案

myContext 为 null,因此当您调用 myContext.getSupportFragmentManager() 时,您会得到一个空指针异常。

Activity 是一个 Context,因此您不需要保留 context 字段。只需调用 .getSupportFragmentManager() 即可。

(哦,并完全删除 myContext,这样您就不会在调用 findViewById() 时发生同样的崩溃。)

关于android - 空对象引用上的 FragmentActivity.getSupportFragmentManager()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com