[编辑]当我尝试在Intent构造函数中使用“this”切换出“v.getContext()”时,Eclipse说它无法构建应用程序。调用onClick()方法时当前的View是否传递给它?
[编辑2]我可能已经找到了问题所在。可能是因为我在使用Android 2.3.3模拟器时使用的是适用于Android 3.0的布局。就是那个,或者我只使用字符串数组而不是整数数组。
在我的Android应用中,当用户点击按钮将其带到选项卡式活动时,我正在运行的模拟器(使用Android 2.3.3)强制关闭我的应用。这是最初的活动。
DungeonsDragonsAppActivity.java文件
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
public class DungeonsDragonsAppActivity extends Activity {
/* Use this for debugging. */
private static final String DEBUG_TAG = "DnDAppDebugLogging";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* The main buttons... Yeah. */
final Button create_character_button = (Button) findViewById(R.id.createCharacterButton);
final Button open_character_button = (Button) findViewById(R.id.openCharacterButton);
final Button retrieve_character_button = (Button) findViewById(R.id.RetrieveCharacterButton);
/* The button for actually creating the character.. */
create_character_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent intent = new Intent(v.getContext(),
CreateCharacterTabsActivity.class);
startActivityForResult(intent, 0);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Click failed", e);
}
}
});
/* The button for opening a pre-existing character. */
open_character_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(DungeonsDragonsAppActivity.this,
"You will be able to open a character... Eventually.",
Toast.LENGTH_SHORT).show();
}
});
/* The button for importing from D & D insider. */
retrieve_character_button
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(
DungeonsDragonsAppActivity.this,
"You will be able to import characters... Hopefully.",
Toast.LENGTH_SHORT).show();
}
}
);
}
}
当用户点击按钮创建新角色时,它应该加载此活动:
CreateCharacterTabsActivity.java
package com.androidGuy.DnDApp;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;
public class CreateCharacterTabsActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Initialize a TabSpec for each tab and add it to the TabHost.
spec = tabHost.newTabSpec("scores");
spec.setContent(new Intent().setClass(this,
CharacterAbilityScoresActivity.class));
spec.setIndicator("Scores");
tabHost.addTab(spec);
intent = new Intent().setClass(this, CharacterClassActivity.class);
spec = tabHost.newTabSpec("classes");
spec.setContent(intent);
spec.setIndicator("Classes");
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
它使用此XML文件来呈现选项卡(我从Android开发教程之一中获取了它)。
tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1">
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
</TabWidget>
</LinearLayout>
</TabHost>
加载该活动后,还应将这些活动加载到选项卡中。
CharacterAbilityScoresActivity.java
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View;
public class CharacterAbilityScoresActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ability_score_spinners); // Set the scene.
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.ability_scores, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/* The ability score spinners */
Spinner strengthSpinner = (Spinner) findViewById(R.id.StrengthSpinner);
strengthSpinner.setAdapter(adapter);
strengthSpinner
.setOnItemSelectedListener(new abilityScoreListSelectedListener());
Spinner constitutionSpinner = (Spinner) findViewById(R.id.ConstitutionSpinner);
constitutionSpinner.setAdapter(adapter);
constitutionSpinner
.setOnItemSelectedListener(new abilityScoreListSelectedListener());
}
/* Now let's see what the user selects. */
class abilityScoreListSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"You selected an ability score. Way to go.",
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing... For now.
}
}
}
ability_score_spinners.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent">
<TableLayout android:layout_height="match_parent" android:id="@+id/tableLayout1" android:layout_width="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="match_parent">
<Spinner android:layout_height="wrap_content" android:minWidth="10dip" android:layout_width="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:id="@+id/StrengthSpinner" android:entries="@array/ability_scores" android:prompt="@string/ability_score_prompt"></Spinner>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Spinner android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/ConstitutionSpinner" android:entries="@array/ability_scores" android:prompt="@string/ability_score_prompt"></Spinner>
</TableRow>
<TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableRow>
<TableRow android:layout_height="wrap_content" android:id="@+id/tableRow3" android:layout_width="wrap_content">
</TableRow>
</TableLayout>
<TableLayout android:layout_height="match_parent" android:id="@+id/tableLayout2" android:layout_width="match_parent">
<TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="@string/strengthText" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/StrengthText"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="@string/constitionText"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableRow>
<TableRow android:id="@+id/tableRow8" android:layout_width="wrap_content" android:layout_height="wrap_content">
</TableRow>
</TableLayout>
</LinearLayout>
CharacterClassesActivity.java
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class CharacterClassActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cha_classes);
final RadioGroup classesButtons = (RadioGroup) findViewById(R.id.radioGroup1);
classesButtons
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
TextView leTextView = (TextView) findViewById(R.id.chooseClassTextView);
if (checkedId != -1) {
RadioButton rb = (RadioButton) findViewById(checkedId);
if (rb != null) {
leTextView.setText("You chose: " + rb.getText());
}
} else {
leTextView.setText("Choose your class");
}
}
});
}
}
cha_classes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Choose your class" android:id="@+id/chooseClassTextView"></TextView>
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ClericClassButton" android:text="Cleric"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FighterClassButton" android:text="Fighter"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/PaladinClassButton" android:text="Paladin"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:text="Ranger" android:layout_height="wrap_content" android:id="@+id/RangerClassButton"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:text="Rogue" android:layout_height="wrap_content" android:id="@+id/RogueClassButton"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:text="Warlock" android:layout_height="wrap_content" android:id="@+id/WarlockClassButton"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:text="Warlord" android:layout_height="wrap_content" android:id="@+id/WarlordClassButton"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:text="Wizard" android:layout_height="wrap_content" android:id="@+id/WizardClassButton"></RadioButton>
</RadioGroup>
</LinearLayout>
能力分数应位于第一个选项卡中,而字符类应位于第二个选项卡中。我在res / values /目录中编写了一个整数数组,并尝试在与能力分数一起使用的微调器中使用它。
起初我认为这是标签布局及其处理方式。它应该做的就是设置选项卡并用适当的活动填充它。我还以为我在CreateCharacterTabsActivity中缺少一个setContentView()方法。但事实并非如此。我还认为这是由于我的清单缺少一些活动条目,但这也是不正确的。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" package="com.androidGuy.DnDApp">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DungeonsDragonsAppActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateCharacterTabsActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.category.LAUNCHER"></action>
</intent-filter>
</activity>
<activity android:name=".CharacterAbilityScoresActivity"></activity>
<activity android:name=".CharacterClassActivity"></activity>
</application>
</manifest>
然后,似乎错误可能与我如何处理微调器的整数数据有关。我创建了一个ArrayAdapter来接受整数数据,并在微调器中设置数据。不过,当我运行模拟器时,它仍然会崩溃。
这是崩溃时的LogCat堆栈跟踪。
ERROR/AndroidRuntime(363): FATAL EXCEPTION: main
ERROR/AndroidRuntime(363): java.lang.NullPointerException
ERROR/AndroidRuntime(363): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
ERROR/AndroidRuntime(363): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
ERROR/AndroidRuntime(363): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.widget.TableRow.getColumnsWidths(TableRow.java:308)
ERROR/AndroidRuntime(363): at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
ERROR/AndroidRuntime(363): at android.widget.TableLayout.measureVertical(TableLayout.java:465)
ERROR/AndroidRuntime(363): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
ERROR/AndroidRuntime(363): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
ERROR/AndroidRuntime(363): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
ERROR/AndroidRuntime(363): at android.view.View.measure(View.java:8313)
ERROR/AndroidRuntime(363): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
ERROR/AndroidRuntime(363): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
ERROR/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:3683)
ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:507)
ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ERROR/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
感谢您提供的任何见解。
答案 0 :(得分:1)
您似乎在清单中定义了两个启动器活动:
...
<activity android:name=".DungeonsDragonsAppActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateCharacterTabsActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.category.LAUNCHER"></action>
</intent-filter>
</activity>
...
这可能会导致TabManager崩溃。
答案 1 :(得分:0)
小心使用内部类中的this
!我经常犯这个错误,通常与传递Context
有关!
请务必使用正确的语法转到“外部”this
,例如MyOuterClass.this
。