2013-12-18 52 views
1

我真的很新的Java和Android,我大部分是自學的。我需要幫助解決這個問題。這可能是一些非常愚蠢,明顯或愚蠢的東西,但是嘿,我必須從某個地方開始吧? 我的應用程序在啓動時崩潰。 我嘗試運行我的應用程序後,在logcat中收到此消息。打開跟蹤文件時出錯。沒有這樣的文件或目錄[2]

這是我的代碼,它是一個練習,我應該做一個小應用程序,您可以在其中顯示一些國家,然後輸入他們的首字母。我使用3個按鈕,2個文字瀏覽和1個edittext。

MainActivity.java

public class MainActivity extends Activity { 
public int cuenta = 0; 
public TextView questionlabel = (TextView) findViewById(R.id.textView1); 
public TextView resultadolabel = (TextView) findViewById(R.id.textView2); 
public Button startbutton = (Button) findViewById(R.id.button1); 
public Button checkbutton = (Button) findViewById(R.id.button2); 
public Button nextbutton = (Button) findViewById(R.id.button3); 
public EditText inputbox = (EditText) findViewById(R.id.editText1); 

public String[] mPaises = { 
     "Canada", 
     "Estados Unidos", 
     "Mexico", 
     "Guatemala", 
     "Belice"}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    startbutton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      //start button 
    questionlabel.setText(mPaises[cuenta]); 
     }}); 

    checkbutton.setOnClickListener(new View.OnClickListener() { 


     @Override 
     public void onClick(View v) { 
      // check button 
      if (cuenta == 0) { 
       if (inputbox.toString() == "Ottawa") { 
        resultadolabel.setText("Correcto");} 
       else {resultadolabel.setText("Respuesta correcta: Ottawa");}} 
      else if (cuenta == 1) { 
       if (inputbox.toString() == "Washington") { 
        resultadolabel.setText("Correcto");} 
       else {resultadolabel.setText("Respuesta correcta: Washington");}} 
      else if (cuenta == 2) { 
       if (inputbox.toString() == "Mexico") { 
        resultadolabel.setText("Correcto");} 
       else {resultadolabel.setText("Respuesta correcta: Mexico");}} 
      else if (cuenta == 3) { 
       if (inputbox.toString() == "Guatemala") { 
        resultadolabel.setText("Correcto");} 
       else {resultadolabel.setText("Respuesta correcta: Guatemala");}} 
      else if (cuenta == 4) { 
       if (inputbox.toString() == "Belmopan") { 
        resultadolabel.setText("Correcto");} 
       else {resultadolabel.setText("Respuesta correcta: Belmoopan");}} 
     } 
    }); 

    nextbutton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // LO QUE HACE EL NEXT BUTTON 
      cuenta = cuenta + 1; 
      questionlabel.setText(mPaises[cuenta]); 


     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

}

這是我Activity_Main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/editText1" 
    android:inputType="text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="129dp" 
    android:ems="10" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/editText1" 
    android:layout_alignLeft="@+id/editText1" 
    android:layout_marginBottom="42dp" 
    android:layout_marginLeft="16dp" 
    android:text="@string/Start" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="63dp" 
    android:text="@string/pais" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button1" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_toRightOf="@+id/button1" 
    android:text="@string/check" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button2" 
    android:layout_toRightOf="@+id/button2" 
    android:text="@string/next" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button1" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_marginBottom="21dp" 
    android:text="@string/mensaje" /> 

我的清單

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.capitales.del.mundo" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

什麼是錯誤? –

回答

1

您需要第一虛增您的佈局,然後檢索您的UI元素,否則findViewById回報null,因此您的應用程序崩潰,在這條線所引起的NullPointerException

startbutton.setOnClickListener 


因此,使用 setContentView(R.layout.activity_main);來擴充您的佈局,然後獲取您的UI元素。

public TextView questionlabel; 
public TextView resultadolabel; 
public Button startbutton; 
public Button checkbutton; 
public Button nextbutton; 
public EditText inputbox;  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    questionlabel = (TextView) findViewById(R.id.textView1); 
    resultadolabel = (TextView) findViewById(R.id.textView2); 
    startbutton = (Button) findViewById(R.id.button1); 
    checkbutton = (Button) findViewById(R.id.button2); 
    nextbutton = (Button) findViewById(R.id.button3); 
    inputbox = (EditText) findViewById(R.id.editText1); 
    /****/ 
} 

注:

  • if (inputbox.toString() == "Ottawa")不要使用==比不上Strings內容。改爲使用equals()
  • How do I compare Strings in Java ?
  • 下一次,包括您的logcat的問題
0

寫所有的這兩行代碼後,

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

因爲你怎麼能找回ID或前一個元素它被創建。

但是你可以在它之前聲明它。但是在創建活動之後,必須檢索id。

相關問題