2011-06-21 49 views
1

嘿傢伙我只是寫了一個名稱getter應用程序,因爲我學習的是android,當它加載到模擬器中時我被迫關閉應用程序。我很新,所以這可能很簡單。剛入門,我簡單的名字getter應用程序崩潰

namegetter.java

package edu.calpoly.android.lab1; 
// tools 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.LabeledIntent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class NameGetter extends Activity implements android.view.View.OnClickListener{ 
    // variables 
    private android.widget.EditText textEdit_Name; 
    private android.widget.Button button_Submit; 
    private String string_Name; 
    private android.content.Intent intent_GetText; 
    private android.os.Bundle bundle; 
    private android.widget.TextView textView_Top; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.name_getter); 

     // assign local variables 
     textEdit_Name = (EditText) this.findViewById(R.id.editText_Name); 
     button_Submit = (Button) this.findViewById(R.id.button_Submit); 
     textView_Top = (TextView) this.findViewById(R.id.LABEL1); 
     button_Submit.setOnClickListener(this); 

     bundle = this.getIntent().getExtras(); 

     bundle.getString(string_Name); 
     textView_Top.setText(string_Name); 


    } 

    public void onClick(View v) { 
     string_Name = textEdit_Name.getText().toString(); 
     // create intent to pass 
     intent_GetText= new Intent(this, HelloWorld.class); 
     // pass to hello world activity 
     intent_GetText.putExtra(string_Name, textEdit_Name.getText().toString()); 
     // initiate 
     this.startActivity(intent_GetText); 
    } 
} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="edu.calpoly.android.lab1" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="12" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name="NameGetter" 
        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="HelloWorld" ></activity> 

    </application> 
</manifest> 
+0

難道您發佈logcat的? 「adb logcat> file_to_write.to」 –

+0

另外,您的name_getter.xml文件 –

回答

2

最可能的原因是getExtras()返回null。

bundle = this.getIntent().getExtras(); 
bundle.getString(string_Name); 

如需更多幫助,我建議收集的logcat(adb logcat)和張貼實際的異常。

1

bundle.getString(string_Name);可能會拋出一個空指針,因爲第一次啓動時,將不會有束加入意向。

+0

將初始值設置爲「Nothing」,仍然停止 – Native

+0

就我所知,您無法設置包的初始值。 –

+0

你爲什麼使用意圖的任何原因?在你展示的這個小例子中不需要它們。 –

1

出一種預感,我會說,你需要刪除這兩行(空指針異常):

bundle = this.getIntent().getExtras(); 
bundle.getString(string_Name); 

但很難說沒有日誌文件。爲什麼?從主屏幕輕擊您的應用程序時,您無需額外付費。

+0

沒關係,請仔細閱讀adb logcat – Native

1

我認爲你的問題在這裏:

bundle = this.getIntent().getExtras(); 
bundle.getString(string_Name); 

什麼是你想怎麼辦呢?

在第二行的位置,string_Name爲空。

+0

我正在嘗試關注[本教程](https://sites.google.com/site/androidappcourse/labs/lab-1)我在5.2。我將名稱變量的初始值設置爲「無」,但它仍然停止 – Native

+0

您誤讀: 「現在我們已完成名稱檢索活動,*讓我們更新** HelloWorld問候活動**以打印輸出用戶輸入的名稱*。在HelloWorld.java的OnCreate方法中: 調用this.getIntent()。getExtras()....「 這些行不在'NameGetter'中。 – dmon

0

事情我看到,以幫助讓生活更美好:

您不必使用完全合格android.widget.EditText myEditText。只需EditText myEditText就可以了。這就是你的進口報表處理;)

你的清單中有一個minSdkVersion =「12」。這可能會帶來一些問題,因爲與您的應用相關的應用只能在裝有android 3.1的設備上運行。嘗試使用minsdk = 7(Android 2.1及以上,更加多產)

可能是很多錯誤的清單,但我只有幾個分鐘看看:)