2011-07-07 56 views
3

[編輯]當我嘗試在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來接受整數數據,並在spinners中設置數據。但是,當我運行模擬器時,它仍然崩潰。

下面是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

create_character_button.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){ 嘗試{ 意向意圖=新意圖(這一點, CreateCharacterTabsActivity.class); startActivityForResult(意向,0); } catch(Exception e){ Log.e(DEBUG_TAG,「Click failed」,e); } } }); 並刪除第二個啓動器活動。主宰者應該只有一個發射器活動。 – Sumant

回答

0

在內部類中使用this要小心!我經常犯這個錯誤,通常是關於傳遞一個Context的東西! 請確保您使用正確的語法到達「外部」this,例如MyOuterClass.this

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崩潰。

相關問題