android开发新手-无法启动新活动

android开发新手-无法启动新活动,android,android-intent,Android,Android Intent,首先-如果我的问题有点愚蠢,对不起,我对Android开发完全陌生 我正在尝试从我的LAUNCHER活动启动一个新活动,并在emulator上运行它,但每次我单击应该启动第二个活动的“下一步”按钮时,我都会收到一条错误消息说:不幸的是,Omis hang man free已停止,然后关闭应用程序。。。 另外,当我在emulator中重新启动同一个应用程序而不从Eclipse重新安装它时,它甚至没有显示第一个屏幕 我甚至不知道我的问题在哪里,所以我附加了5个代码: 第一个名为“活动\打开”的活动布

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

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

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

第一个名为“活动\打开”的活动布局

<?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>
第2个名为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;
}

}
第二个活动布局命名为布局\游戏

<?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>
五,。显示

<?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>

我不是在寻找代码改进,我只是在学习语言,所以请不要给我发送改进代码的建议,我知道有很多-我只想让这段代码正常工作。

问题可能出在意图上。试试这个:

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

问题可能在于意图。试试这个:

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

请在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;
}

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

请在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;
}

}
Intent gameIntent  = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
请去掉饰面;从OpenActivity的onPause方法中删除gvGrid.addviewt;从GameActivity开始,因为gridview不是容器布局,请编写以下代码

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

Intent gameIntent = new    Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
它将解决您的问题。

请删除finish;从OpenActivity的onPause方法中删除gvGrid.addviewt;从GameActivity开始,因为gridview不是容器布局,请编写以下代码

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

Intent gameIntent = new    Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
它将解决您的问题。

尝试在此处更改:

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

尝试在此处更改:

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



新手:在Eclipse中打开LogCat视图以获取堆栈跟踪,并将其发布到此处。11-30 11:22:04.087:E/AndroidRuntime2374:FATAL EXCEPTION:main11-30 11:22:04.087:E/AndroidRuntime2374:java.lang.RuntimeException:无法启动活动组件信息{com.omi.hangmanfree/com.omi.hangmanfree.GameActivity}:java.lang.NullPointerException 11-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.performLaunchActivityActivityThread.java:2180 11-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.HandleLaunchActivityThread.java:2230 11-30 11:22:04.087:E/AndroidRuntime2374:atandroid.app.ActivityThread.access$600ActivityThread.java:14111-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread$H.handleMessageActivityThread.java:1234 11-30 11:22:04.087:E/AndroidRuntime2374:at android.os.Handler.dispatchMessageHandler.java:99 11-30 11:22:04.087:E/AndroidRuntime2374:atandroid.os.Looper.loopLooper.java:13711-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.mainActivityThread.java:5039 11-30 11:22:04.087:E/AndroidRuntime2374:at java.lang.reflect.Method.InvokenativeEnable方法11-30 11:22:04.087:E/AndroidRuntime2374:at java.lang.reflect.Method.invokeMethod.java:511您是新来的:打开Eclipse中的LogCat视图获取堆栈跟踪并发布到此处。11-30 11:22:04.087:E/AndroidRuntime2374:致命异常:main 11-30 11:22:04.087:E/AndroidRuntime2374:java.lang.RuntimeException:无法启动活动组件信息{com.omi.hangmanfree/com.omi.hangmanfree.GameActivity}:java.lang.NullPointerException 11-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.performLaunchActivityActivityThread.java:2180 11-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.HandleLaunchActivityThread.java:2230 11-30 11:22:04.087:E/AndroidRuntime2374:atandroid.app.ActivityThread.access$600ActivityThread.java:14111-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread$H.handleMessageActivityThread.java:1234 11-30 11:22:04.087:E/AndroidRuntime2374:at android.os.Handler.dispatchMessageHandler.java:99 11-30 11:22:04.087:E/AndroidRuntime2374:atandroid.os.Looper.loopLooper.java:13711-30 11:22:04.087:E/AndroidRuntime2374:at android.app.ActivityThread.mainActivityThread.java:5039 11-30 11:22:04.087:E/AndroidRuntime2374:at java.lang.reflect.Method.InvokenativeEnable方法11-30 11:22:04.087:E/AndroidRuntime2374:at java.lang.reflect.Method.invokeMethod.java:511您能告诉我什么吗当前代码中有问题吗?没有帮助。。。同样的问题是你的logcat跟踪来查看你得到的异常。把它贴在我的问题下面我现在看到了。问题不在于意图,而在于GameActivity中的文本视图。当您试图设置其文本时,tt为空。首先尝试创建它:TextView tt=new TextViewgetApplicationContext;,然后您可以调用tt.setText;你能告诉我当前代码有什么问题吗?没有帮助。。。同样的问题是你的logcat跟踪来查看你得到的异常。把它贴在我的问题下面我现在看到了。问题不在于意图,而在于GameActivity中的文本视图。当您试图设置其文本时,tt为空。首先尝试创建它:TextView tt=newtextViewGetAppli
cationContext;,然后您可以调用tt.setText;tnx的提示关于gridview,但它没有帮助我的问题。。。相同的error@omi添加此代码行tt=TextViewfindViewByIdR.id.textView1;在tt.setText_u2;之前;,它会解决你的问题。是的!非常感谢。这是关于gridview的提示的问题…tnx,但它对我的问题没有帮助。。。相同的error@omi添加此代码行tt=TextViewfindViewByIdR.id.textView1;在tt.setText_u2;之前;,它会解决你的问题。是的!非常感谢。这就是问题所在。。。