2012-11-30 45 views
1

第一 - 對不起,如果我的問題有點啞,我是全新的Android開發...新到Android開發 - 着開始新的活動

我試圖從開始一個新的活動我的「 LAUNCHER「活動並在模擬器上運行它,但每次點擊」下一步「按鈕(這應該開始我的第二個活動)時,我會收到一條錯誤消息:」不幸的是,Omis掛機自由已停止「,然後關閉應用程序... 也,當我在模擬器中重新啓動相同的應用程序,而無需從Eclipse重新安裝它,它甚至不顯示我的第一個屏幕...

我甚至沒有知道我的問題在哪裏,所以我附上了5個代碼:

第1'活動佈局(名爲 「activity_open」)

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context=".OpenActivity" 
    android:background="#DCDCDC" > 

    <LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_weight="30" > 

     <TextView 
      android:id="@+id/tvEnterName" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:text="@string/stEnterName" 
      android:textSize="24sp" /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_weight="40" > 

     <EditText 
      android:id="@+id/etNameInput" 
      android:layout_width="250dp" 
      android:layout_height="fill_parent" 
      android:background="#FFFFFF" 
      android:focusableInTouchMode="true" 
      android:gravity="center" 
      android:hint="@string/stHintNameEnter" 
      android:singleLine="true" /> 

    </LinearLayout> 
<LinearLayout 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:layout_weight="30" > 

<Button 
    android:id="@+id/bNext" 
    android:layout_width="75dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/stNext" 
    android:textSize="24sp" /> 
</LinearLayout> 

</LinearLayout> 

第1'活動類(名爲 「OpenActivity」)

package com.omi.hangmanfree; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class OpenActivity extends Activity { 

TextView tvEnterYourName; 
EditText etName; 
Button bContinue; 
String myName, tmp; 
final String errorStr = "\nYou must enter a name!"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_open); 

    initialize(); 

    bContinue.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      myName = etName.getText().toString(); 

      if(etName.getText().toString().equals("") == true) 
      { 
       tvEnterYourName.setText(tmp + errorStr); 
       tvEnterYourName.setTextColor(Color.RED); 
      } 
      else 
      { 
       try { 
        Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY"); 
        startActivity(gameIntent); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
} 

private void initialize() { 
    // TODO Auto-generated method stub 
    tvEnterYourName = (TextView)findViewById(R.id.tvEnterName); 
    etName = (EditText)findViewById(R.id.etNameInput); 
    bContinue = (Button)findViewById(R.id.bNext); 
    myName = "~"; 
    tmp = tvEnterYourName.getText().toString(); 
} 



@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 

    finish(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_open, menu); 
    return true; 
} 

} 

第2'活動類(名爲 「GameActivity」 ) package com.omi.hangmanfree;

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.GridView; 
import android.widget.TextView; 

public class GameActivity extends Activity { 

TextView tt; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 

    GridView gvGrid = (GridView)findViewById(R.id.gvHagdara); 
    tt.setText("_"); 
    gvGrid.addView(tt); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_game, menu); 
    return true; 
} 

} 

第2'活動佈局(名爲 「layout_game」)

<?xml version="1.0" encoding="utf-8" ?> 
<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" 
tools:context=".GameActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" /> 

<GridView 
    android:id="@+id/gvHagdara" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_below="@+id/textView1" 
    android:layout_centerInParent="false" 
    android:numColumns="auto_fit" > 

</GridView> 

</RelativeLayout> 

5.清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.omi.hangmanfree" 
android:versionCode="1" 
android:versionName="1.0" > 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".OpenActivity" 
     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=".GameActivity" 
     android:label="@string/title_activity_game" > 
     <intent-filter> 
      <action android:name="com.omi.hangmanfree.GAMEACTIVITY" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

我不是找代碼改進(我只學習語言),所以請不要發送給我改進代碼的建議(我知道有很多!)---我只是想讓這段代碼工作。

+0

什麼是logcat的說? – vodich

+0

由於您是新手:在Eclipse中打開LogCat視圖以獲取堆棧跟蹤並將其發佈到此處。 – WarrenFaith

+0

logCat說: – omi

回答

4

問題可能出在意圖上。試試這個:

Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class); 
+0

可以嗎?我現在的代碼有什麼問題? –

+0

沒有幫助...相同的problam – omi

+0

發佈你的logcat跟蹤來查看你得到的異常。 –

0

請openActivity取代

Intent gameIntent = new Intent(getApplicationContext(), GameActivity.class); 
startActivity(gameIntent); 
2

請從onPause()方法從OpenActivity刪除finish();和GameActivity刪除gvGrid.addView(tt);因爲GridView控件不是容器佈局和下面寫代碼

Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class); 
startActivity(gameIntent); 

而不是

Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY"); 
startActivity(gameIntent); 

它會解決你的問題。

+0

tnx爲關於gridview的提示,但它並沒有幫助我的問題...同樣的錯誤 – omi

+0

@omi添加此代碼行tt =(TextView)findViewById(R.id.textView1);在tt.setText(「_」)之前,它會解決你的問題。 –

+0

是的!謝謝!那是個問題... – omi

0

嘗試改變這裏:

  Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY"); 
      startActivity(gameIntent); 

   Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class); 
       startActivity(gameIntent);