1

首先 - 对不起,如果我的问题有点愚蠢,我对 Android 开发完全陌生......

我正在尝试从我的“LAUNCHER”活动开始一个新活动并在模拟器上运行它,但是每次我单击“下一步”按钮(应该开始我的第二个活动)时,我都会收到一条错误消息:“不幸的是,Omis hang man free 已经停止”,然后关闭应用程序......另外,当我在模拟器中重新启动同一个应用程序而不从 Eclipse 重新安装它时,它甚至没有向我显示第一个屏幕。 ..

我什至不知道我的问题出在哪里,所以我附上了 5 个代码:

第一个活动布局(名为“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>

第一个活动类(名为“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;
}

}

第二个活动类(名为“GameActivity”)包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;
}

}

第二个活动布局(名为“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>

我不是在寻找代码改进(我只是在学习语言),所以请不要向我发送改进代码的建议(我知道有很多!)---我只是希望这段代码能够工作。

4

4 回答 4

4

问题可能出在 Intent 中。试试这个:

Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
于 2012-11-30T10:47:06.627 回答
2

请从 OpenActivity 的方法中删除finish();onPause()从 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);

它会解决你的问题。

于 2012-11-30T10:47:46.317 回答
0

请在 openActivity 中替换

Intent gameIntent  = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
于 2012-11-30T10:47:43.173 回答
0

尝试在这里更改:

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

                Intent gameIntent = new    Intent(OpenActivity.this, GameActivity.class);
                startActivity(gameIntent);
于 2012-11-30T10:48:02.863 回答