简体   繁体   English

Android应用挂起

[英]Android app hangs

Hey i'm brand new to programming and i need some help, i'm doing a project for school and it's an android app where the user inputs words to a text field and then those words are used to create a story, like mad libs. 嘿,我是编程的新手,我需要一些帮助,我在做一个学校的项目,这是一个Android应用程序,用户在其中将单词输入文本字段,然后使用这些单词来创建故事,例如mad libs 。 I can not seem to see what is wrong with my code here when i debug on my phone it just shows a white screen. 我在手机上调试时似乎看不到我的代码出了什么问题,它只显示白屏。 Could someone point out to me what i'm doing wrong? 有人可以向我指出我在做什么错吗? thanks... 谢谢...

MainActivity.java:

package com.joe.madlibs;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    EditText Noun, Adj, Name, Verb, Verb2, Noun2, Adj2;
    Button Submit;
    TextView Story;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Noun = (EditText) findViewById(R.id.noun1);
        Adj = (EditText) findViewById(R.id.adjective1);
        Name = (EditText) findViewById(R.id.name1);
        Verb = (EditText) findViewById(R.id.verb1);
        Verb2 = (EditText) findViewById(R.id.verb2);
        Noun2 = (EditText) findViewById(R.id.noun2);
        Adj2 = (EditText) findViewById(R.id.adj2);
        String noun = Noun.getText().toString();
        String adj = Adj.getText().toString();
        String name = Name.getText().toString();
        String verb = Verb.getText().toString();
        String verb2 = Verb2.getText().toString();
        String noun2 = Noun2.getText().toString();
        String adj2 = Adj2.getText().toString();
        Submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

    }

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

}

activity_main.xml
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

    <EditText
        android:id="@+id/noun1"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignTop="@+id/linearLayout1"
        android:ems="10"
        android:hint="@string/noun" >
    </EditText>

    <EditText
        android:id="@+id/verb1"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/name1"
        android:layout_alignBottom="@+id/name1"
        android:layout_alignRight="@+id/storyBox"
        android:ems="10"
        android:hint="@string/verb" />

    <EditText
        android:id="@+id/adjective1"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/noun1"
        android:layout_alignBottom="@+id/noun1"
        android:layout_alignLeft="@+id/verb1"
        android:ems="10"
        android:hint="@string/adjective" />

    <EditText
        android:id="@+id/name1"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/noun1"
        android:layout_below="@+id/noun1"
        android:ems="10"
        android:hint="@string/name" />

    <EditText
        android:id="@+id/verb2"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name1"
        android:layout_below="@+id/name1"
        android:ems="10"
        android:hint="@string/verb" />

    <EditText
        android:id="@+id/adj2"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/verb1"
        android:layout_below="@+id/verb1"
        android:ems="10"
        android:hint="Adjective" />

    <EditText
        android:id="@+id/noun2"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/verb2"
        android:layout_below="@+id/verb2"
        android:ems="10"
        android:hint="Noun" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click here to get your story!" />

    <TextView
        android:id="@+id/storyBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/noun2"
        android:layout_below="@+id/button1"
        android:layout_marginTop="18dp"
        android:text="This is where your story will go!"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Joe.madlibs"
    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="com.Joe.madlibs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Umm... where are you instantiating your Submit button before trying to set an onClickListener on it? 嗯...在尝试在其上设置onClickListener之前,您在哪里实例化Submit按钮? I see where you declare it, but you aren't instantiating it. 我看到了在哪里声明它,但没有实例化它。

Add: 加:

Submit = (Button)findViewById(R.id.button1);

right before 就在之前

Submit.setOnClickListener(new View.OnClickListener() {

and watch. 看。 It's gonna be like magic. 就像魔术一样。

One more thing about coding conventions (style): when you name your instance variables, use the headlessCamelCase convention. 关于编码约定(样式)的另一件事:当您命名实例变量时,请使用headlessCamelCase约定。 Names with CamelCase/InitCaps (first letter of each word capitalized) should only be used when you name your class (the pattern for an object) as opposed to when you refer to an instance of that class (the actual object). 具有CamelCase / InitCaps的名称(每个单词大写的首字母)仅在您为类命名(对象的模式)时使用,而不是在引用该类的实例(实际对象)时使用。

Wrong: 错误:

// An instance of Button class
Button Submit;

Right: 对:

// An instance of Button class:
Button submit;

When it comes to compound names (more than one word), headlessCamelCase means you capitalize the first letter of each word, except the first word. 当涉及复合名称(一个以上的单词)时,headlessCamelCase表示您将每个单词的首字母大写,首字母除外。

Right: 对:

// An instance of Button class:
Button myVeryElderlyMotherJustSatUponNapoleonsSubmitButton;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM