我正在尝试在标签布局中制作一个简单的Android gridView。但是,我的应用程序最终崩溃了。
这是我的活动标签:
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
任何帮助将不胜感激!
答案 0 :(得分:2)
确定。所以我导入了你的代码并让它运行起来。
有几件事情引起了混乱,所以我只是粘贴工作代码并解释我改变了什么。
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;
}
}
}
根据我的经验,碎片和活动有着特殊的关系。我总是用两个布局xml文件设计片段/活动关系;一个简单的活动容器和一个片段容器,其中显示了大部分内容。
所以这里是名为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_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的片段。
现在回到代码中。当您创建片段时,首先需要对布局进行膨胀,然后可以在该视图上调用findViewById来访问子视图。然后返回onCreateView方法的膨胀视图。
另一件事,我总是使用support.v4.app.Fragment和supportFragmentManager取得成功,所以我也修改了它。
答案 1 :(得分:0)
根据日志,错误就在这一行。
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,numbers1);
因此,其中一项必须为null:
getActivity()
android.R.layout.simple_list_item_1
numbers1
您可以通过打印每个或通过调试器来测试它是哪一个。
最有可能的是,这就是这一行:
android.R.layout.simple_list_item_1