gpt4 book ai didi

java - NullPointEreException 选项卡布局 GridView

转载 作者:行者123 更新时间:2023-12-02 05:51:39 25 4
gpt4 key购买 nike

我正在尝试在选项卡布局中制作一个简单的 Android gridView。但是,我的应用程序最终崩溃了。

这是我的 Activity ,其中包含选项卡:

EventsActivity.java

package com.grace.main;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class EventsActivity extends FragmentActivity implements ActionBar.TabListener {
//needs R string
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);

//Set up the action bar to show tabs
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//Add a tab to the action bar
//needs R.string values
actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
//restores the previously serialised current tab position
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)){
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}

@Override
public void onSaveInstanceState(Bundle outState){
//Serialise the current tab position
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}

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

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Uses container view
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition()+1);
fragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}

@Override
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
//Do nothing
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Do nothing
}

//dummy test
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "placeholder_text";
GridView gridView;
static final String[] numbers1 = new String[] {

"1", "2", "3", "4", "5"
};
static final String[] numbers2 = new String[] {

"6", "7", "8", "9", "10"
};
static final String[] numbers3 = new String[] {

"11", "12", "13", "14", "15"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
GridView gridview = (GridView)getActivity() .findViewById(R.id.gridview);

switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers1);
gridView.setAdapter(adapter1);
break;
case 2:
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers2);
gridView.setAdapter(adapter2);
break;
case 3:
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers3);
gridView.setAdapter(adapter3);
break;
default:
//textView.setText("HELLO");
break;
}
return gridview;
}
}
}

这是我的布局 xml:Activity_events.xml

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="capstone.DGW.Main.EventsActivity"
tools:ignore="MergeRootFrame" >

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >

</GridView>
</FrameLayout>

我的 LogCat 错误是:

05-06 10:35:47.637: E/AndroidRuntime(16838):    Caused by: java.lang.NullPointerException

05-06 10:35:47.637: E/AndroidRuntime(16838): at com.grace.main.EventsActivity$DummySectionFragment.onCreateView(EventsActivity.java:99)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.Fragment.performCreateView(Fragment.java:1700)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.BackStackRecord.run(BackStackRecord.java:684)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.Activity.performStart(Activity.java:5240)

05-06 10:35:47.637: E/AndroidRuntime(16838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)

05-06 10:35:47.637: E/AndroidRuntime(16838): ... 11 more

如有任何帮助,我们将不胜感激!

最佳答案

好的。所以我导入了你的代码并让它工作。

有几件事导致了困惑,因此我将粘贴工作代码并解释我更改的内容。

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class EventsActivity extends FragmentActivity implements ActionBar.TabListener {
//needs R string
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_container);

//Set up the action bar to show tabs
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//Add a tab to the action bar
//needs R.string values
actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
//restores the previously serialised current tab position
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)){
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}

@Override
public void onSaveInstanceState(Bundle outState){
//Serialise the current tab position
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}

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

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Uses container view
android.support.v4.app.Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition()+1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.events_container, fragment).commit();

//getSupportFragmentManager().beginTransaction()
//.add(R.id.assessment_container, fragment).commit();

}

@Override
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
//Do nothing
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Do nothing
}

//dummy test
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "placeholder_text";
GridView gridView;
static final String[] numbers1 = new String[] {

"1", "2", "3", "4", "5"
};
static final String[] numbers2 = new String[] {

"6", "7", "8", "9", "10"
};
static final String[] numbers3 = new String[] {

"11", "12", "13", "14", "15"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_events,
container, false);

GridView gridview = (GridView)rootView.findViewById(R.id.gridview);

switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers1);
gridview.setAdapter(adapter1);
break;
case 2:
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers2);
gridview.setAdapter(adapter2);
break;
case 3:
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, numbers3);
gridview.setAdapter(adapter3);
break;
default:
//textView.setText("HELLO");
break;
}
return rootView;
}
}
}

根据我的经验, fragment 和 Activity 有着特殊的关系。我总是用两个布局 xml 文件来设计 fragment/Activity 关系;一个简单的 Activity 容器和一个显示大部分内容的 fragment 容器。

这是一个简单的 Activity 布局,名为activity_events_layout.xml

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/events_container"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>

记下 id/events_container。该引用在 fragment 事务期间使用。现在进入布局fragment_events.xml:

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="capstone.DGW.Main.EventsActivity"
tools:ignore="MergeRootFrame" >

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >

</GridView>
</FrameLayout>

这是包含 Gridview 的 fragment 。

现在回到代码。创建 fragment 时,首先需要膨胀布局,然后可以在该 View 上调用 findViewById 来访问 subview 。然后为 onCreateView 方法返回膨胀的 View 。

另一件事,我一直在 support.v4.app.Fragment 和 supportFragmentManager 方面取得成功,所以我也对其进行了修改。

关于java - NullPointEreException 选项卡布局 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23484179/

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