gpt4 book ai didi

java - 致命异常 : main ViewPager mPager = (ViewPager) findViewById(R. id.view_pager)

转载 作者:行者123 更新时间:2023-12-01 13:25:07 25 4
gpt4 key购买 nike

我在final ViewPager mPager = (ViewPager) findViewById(R.id.view_pager); 行上遇到致命异常

但是我无法理解为什么会发生这种情况。我已经在 XML 中声明了具有正确 ID 的 viewPager(如下所示),但是应用程序在尝试实例化它时仍然崩溃。

非常感谢任何建议。

日志猫:

02-17 11:54:22.547: E/AndroidRuntime(13618): FATAL EXCEPTION: main
02-17 11:54:22.547: E/AndroidRuntime(13618): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.SAXParserAsyncTaskActivity}: java.lang.NullPointerException
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.ActivityThread.access$600(ActivityThread.java:156)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.os.Looper.loop(Looper.java:153)
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.ActivityThread.main(ActivityThread.java:5297)
02-17 11:54:22.547: E/AndroidRuntime(13618): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 11:54:22.547: E/AndroidRuntime(13618): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 11:54:22.547: E/AndroidRuntime(13618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-17 11:54:22.547: E/AndroidRuntime(13618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-17 11:54:22.547: E/AndroidRuntime(13618): at dalvik.system.NativeStart.main(Native Method)
02-17 11:54:22.547: E/AndroidRuntime(13618): Caused by: java.lang.NullPointerException
02-17 11:54:22.547: E/AndroidRuntime(13618): at android.app.Activity.findViewById(Activity.java:1852)
02-17 11:54:22.547: E/AndroidRuntime(13618): at com.idg.omv.ui.phone.SAXParserAsyncTaskActivity.<init>(SAXParserAsyncTaskActivity.java:67)

XML

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical" >
</LinearLayout>

<ListView
android:id="@+id/left_drawer"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/darkgrey"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:textStyle="bold" />

<ListView
android:id="@+id/videosListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAFFFFFF" >
</RelativeLayout>

<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true" >

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

<ImageButton
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:focusable="false"
android:src="@drawable/scroll_lt_arrow" />

<ImageButton
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:focusable="false"
android:src="@drawable/scroll_rt_arrow" />
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

Java:

public class SAXParserAsyncTaskActivity extends Activity implements
OnClickListener, OnItemClickListener {

ListView listView;
List<Cmd> videos = new ArrayList<Cmd>();

CustomListViewAdapter listViewAdapter;

static final String URL = "http://exampleurl.com";
public static final String LIBRARY = "Library";
private ActionBarDrawerToggle actionBarDrawerToggle;
public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
public static final String VIDEO_ID = "o7VVHhK9zf0";
private int mCurrentTabPosition = NO_CURRENT_POSITION;
private static final int NO_CURRENT_POSITION = -1;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private String[] drawerListViewItems;
ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);

最佳答案

onCreate() 方法之前声明 ViewPager 对象,并在 onCreate() 方法内初始化该对象,如下所示...

ViewPager mPager; 

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

mPager = (ViewPager) findViewById(R.id.view_pager);
}

关于java - 致命异常 : main ViewPager mPager = (ViewPager) findViewById(R. id.view_pager),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836462/

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