在我的小应用程序中,我正在尝试将视图放入此活动的选项卡中。我首先尝试做的是设置视图,其中一些代码可以在其他类中找到(在其他文件中)。这是构建选项卡并设置代码的类。
CreateCharacterTabsActivity.java
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TableLayout;
import android.content.Context;
import android.content.Intent;
import android.widget.TabHost.TabContentFactory;
public class CreateCharacterTabsActivity extends Activity implements OnTabChangeListener {
private TabHost tabHost;
private TableLayout scoreTable;
private LinearLayout raceLayout;
private CharacterAbilityScoresActivity abilityScoresActivity;
// Tab tags: got to love them.
private static final String ABILITY_SCORES_TAB = "scores";
private static final String RACE_TAB = "race";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
// Set up them tabs.
tabHost = (TabHost) findViewById(android.R.id.tabhost);
// Set up the tabhost.
tabHost.setup();
tabHost.setOnTabChangedListener(this);
scoreTable = (TableLayout) abilityScoresActivity.getAbilityScoreTable();
raceLayout = (LinearLayout) findViewById(R.id.raceLayout);
// Add views to the tab host.
tabHost.addTab(tabHost.newTabSpec(ABILITY_SCORES_TAB).setIndicator("Scores").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return scoreTable;
}
}));
tabHost.addTab(tabHost.newTabSpec(RACE_TAB).setIndicator("Races").setContent(new TabContentFactory() {
public View createTabContent(String arg1) {
return raceLayout;
}
}));
// I have heard this is a hack brought upon by a bug.
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);
}
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
}
}
它从这个类中获取其他代码。
CharacterAbilityScoresActivity.java
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.content.Context;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
/* This is for character class scores, settings, and... Stuff. */
import com.androidGuy.DnDApp.CharacterData;
public class CharacterAbilityScoresActivity extends Activity {
CharacterData characterData = new CharacterData();
private Spinner strengthSpinner;
private Spinner dexteritySpinner;
private Spinner constitutionSpinner;
private Spinner intelligenceSpinner;
private Spinner wisdomSpinner;
private Spinner charismaSpinner;
private ArrayAdapter<?> adapter;
private TableLayout abilityScoreTable;
public TableLayout getAbilityScoreTable() {
return abilityScoreTable;
}
/* Le constructor. Set up all of the fields (the adapters and the spinner boxes) and return
* a tabletLayout objects (or at least, I hope that's how it'll work). */
CharacterAbilityScoresActivity() {
final LayoutInflater Die_Fledermaus = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
abilityScoreTable = (TableLayout) Die_Fledermaus.inflate(R.layout.ability_score_spinners, null);
adapter = ArrayAdapter.createFromResource(Die_Fledermaus.getContext(), R.array.string_ability_scores, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/* The ability score spinners */
strengthSpinner = (Spinner) findViewById(R.id.StrengthSpinner);
strengthSpinner.setAdapter(adapter);
strengthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
characterData.setStrengthScore(getResources().getIntArray(R.array.ability_scores)[(int) strengthSpinner.getItemIdAtPosition(arg2)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
dexteritySpinner = (Spinner) findViewById(R.id.dexteritySpinner);
dexteritySpinner.setAdapter(adapter);
dexteritySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
characterData.setDexterityScore(getResources().getIntArray(R.array.ability_scores)[(int) dexteritySpinner.getItemIdAtPosition(arg2)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
constitutionSpinner = (Spinner) findViewById(R.id.ConstitutionSpinner);
constitutionSpinner.setAdapter(adapter);
constitutionSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
characterData.setConstitutionScore(getResources().getIntArray(R.array.ability_scores)[(int) constitutionSpinner.getItemIdAtPosition(pos)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
intelligenceSpinner = (Spinner) findViewById(R.id.intelligenceSpinner);
intelligenceSpinner.setAdapter(adapter);
intelligenceSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
characterData.setIntelligenceScore(getResources().getIntArray(R.array.ability_scores)[(int) intelligenceSpinner.getItemIdAtPosition(pos)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
wisdomSpinner = (Spinner) findViewById(R.id.WisdomSpinner);
wisdomSpinner.setAdapter(adapter);
wisdomSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
characterData.setWisdomScore(getResources().getIntArray(R.array.ability_scores)[(int) wisdomSpinner.getItemIdAtPosition(arg2)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
charismaSpinner = (Spinner) findViewById(R.id.CharismaSpinner);
charismaSpinner.setAdapter(adapter);
charismaSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
characterData.setCharismaScore(getResources().getIntArray(R.array.ability_scores)[(int) charismaSpinner.getItemIdAtPosition(arg2)]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
假设要发生的是CharacterAbilityScoresActivity
中的构造函数创建所有旋转框并为其分配适配器。然后我尝试返回tableLayout,它将createTabContent()
方法保存在CreateCharacterTabsActivity
中。但是,会发生在tabHost.setup()
周围发生错误(堆栈跟踪表示错误发生的地方)。
[edit]为了进一步提供帮助,这里是堆栈跟踪。
07-16 13:40:47.100: ERROR/AndroidRuntime(386): FATAL EXCEPTION: main
07-16 13:40:47.100: ERROR/AndroidRuntime(386): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidGuy.DnDApp/com.androidGuy.DnDApp.CreateCharacterTabsActivity}: java.lang.NullPointerException
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.os.Looper.loop(Looper.java:123)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at java.lang.reflect.Method.invoke(Method.java:507)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at dalvik.system.NativeStart.main(Native Method)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): Caused by: java.lang.NullPointerException
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at com.androidGuy.DnDApp.CreateCharacterTabsActivity.onCreate(CreateCharacterTabsActivity.java:34)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-16 13:40:47.100: ERROR/AndroidRuntime(386): ... 11 more
答案 0 :(得分:1)
虽然没有堆栈跟踪和一些\ infty数量的代码,但这看起来很简单:
setContentView(R.layout.simple_layout);
// Set up them tabs.
tabHost = (TabHost) findViewById(android.R.id.tabhost);
您必须意识到findViewById
在任何XML中都找不到随机视图,但在屏幕上查找视图。
由于ID为tabhost
的视图位于tabs.xml中,并且据我所知,您在屏幕上只有simple_layout
,此时变量tabHost
将为{ {1}}。
下一行:
null
会给你一个tabHost.setup();
。