gpt4 book ai didi

java - 空指针异常选项卡滑动 View

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:18 25 4
gpt4 key购买 nike

我的应用程序在我的 fragment 中包含滑动 View 。我在其中输入了滑动 View 代码的应用程序,显然存在 NullPointerException 错误,但我找不到它的确切代码含义。有人可以帮忙吗?

这是类(class):

 package com.noura.luba;

import java.util.List;
import java.util.Stack;

import android.app.ActionBar;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class Degree_Programs extends FragmentActivity {

ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.degree_programs);

final ActionBar actionBar = getActionBar();
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});

// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}

public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction ft) {
// hide the given tab
}

public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction ft) {
// probably ignore this event
}
};

// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 4; i++) {

if (i == 0)
actionBar.addTab(actionBar.newTab()
.setCustomView(R.layout.study_cycle)
.setTabListener(tabListener));
// .setText("Tab " + (i + 1))

if (i == 1)
actionBar.addTab(actionBar.newTab().setCustomView(R.layout.lmd)
.setTabListener(tabListener));

if (i == 2)
actionBar.addTab(actionBar.newTab()
.setCustomView(R.layout.business_administration)
.setTabListener(tabListener));
if (i == 3)
actionBar.addTab(actionBar.newTab().setText("Tab " + (i + 1))
.setTabListener(tabListener));
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection

switch (item.getItemId()) {
/*
* case R.id.settings: Intent intentSettings = new
* Intent(getApplicationContext(), Settings.class);
* startActivity(intentSettings); return true;
*/
case R.id.information:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.information);
return true;

case R.id.logOut:

Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
return true;

case R.id.email:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");
/*
* i.putExtra(Intent.EXTRA_EMAIL, new String[] {
* ANDROID_SUPPORT_EMAIL });
*/
i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");

startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
return true;

default:
return super.onOptionsItemSelected(item);
}
}

public Intent createEmailOnlyChooserIntent(Intent source,
CharSequence chooserTitle) {
Stack<Intent> intents = new Stack<Intent>();
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
"noura.h.hadi@gmail.com", null));
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(i, 0);

for (ResolveInfo ri : activities) {
Intent target = new Intent(source);
target.setPackage(ri.activityInfo.packageName);
intents.add(target);
}

if (!intents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(intents.remove(0),
chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[intents.size()]));

return chooserIntent;
} else {
return Intent.createChooser(source, chooserTitle);
}

}
}

这是日志猫

12-03 02:27:38.216: E/AndroidRuntime(2255): FATAL EXCEPTION: main
12-03 02:27:38.216: E/AndroidRuntime(2255): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noura.luba/com.noura.luba.Degree_Programs}: java.lang.NullPointerException
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.os.Looper.loop(Looper.java:137)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-03 02:27:38.216: E/AndroidRuntime(2255): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 02:27:38.216: E/AndroidRuntime(2255): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 02:27:38.216: E/AndroidRuntime(2255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 02:27:38.216: E/AndroidRuntime(2255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 02:27:38.216: E/AndroidRuntime(2255): at dalvik.system.NativeStart.main(Native Method)
12-03 02:27:38.216: E/AndroidRuntime(2255): Caused by: java.lang.NullPointerException
12-03 02:27:38.216: E/AndroidRuntime(2255): at com.noura.luba.Degree_Programs.onCreate(Degree_Programs.java:30)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.Activity.performCreate(Activity.java:5104)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-03 02:27:38.216: E/AndroidRuntime(2255): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-03 02:27:38.216: E/AndroidRuntime(2255): ... 11 more

任何帮助将不胜感激

最佳答案

这就是导致问题的原因

mViewPager.setOnPageChangeListener(...);

在调用任何方法之前,您必须先初始化mViewPager

关于java - 空指针异常选项卡滑动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27270955/

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