无法创建Android应用程序

时间:2014-04-21 00:15:51

标签: java android

我试图制作一个你用你的名字输入的应用程序,无论你的名字是什么名字,它都会打印出你的名字,但每当我尝试打开应用程序时,应用程序都会在我的手机或模拟器上打开不幸的是helloworld(我原来的应用名称)已经停止了。 这是我的主要代码;任何帮助表示赞赏

package com.example.helloworld;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class Main extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

final EditText namefield = (EditText)findViewById(R.id.name) ;
final EditText nameview = (EditText)findViewById(R.id.nameout);
final Button button2 = (Button)findViewById(R.id.calc);
button2.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {

        nameview.setText("Hello "+namefield.getText());

    }

});
}       

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

Fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
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="com.example.helloworld.Main$PlaceholderFragment" >

<Button
    android:id="@+id/calc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/name"
    android:layout_below="@+id/name"
    android:layout_marginTop="125dp"
    android:text="@string/calc" />

<TextView
    android:id="@+id/output"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/name"
    android:layout_alignRight="@+id/calc"
    android:layout_centerVertical="true"
    android:text="@string/name" />

<EditText
    android:id="@+id/nameout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/calc"
    android:layout_centerHorizontal="true"
    android:ems="10" android:inputType="text"/>

<EditText
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="text" >

    <requestFocus />
</EditText>

</RelativeLayout>

记录猫

04-21 21:10:20.678: E/AndroidRuntime(1100): FATAL EXCEPTION: main
04-21 21:10:20.678: E/AndroidRuntime(1100): Process: com.example.helloworld, PID: 1100
04-21 21:10:20.678: E/AndroidRuntime(1100): java.lang.RuntimeException: Unable to start                       activity ComponentInfo{com.example.helloworld/com.example.helloworld.Main}:        java.lang.NullPointerException
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.os.Looper.loop(Looper.java:136)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at java.lang.reflect.Method.invoke(Method.java:515)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at dalvik.system.NativeStart.main(Native Method)
04-21 21:10:20.678: E/AndroidRuntime(1100): Caused by: java.lang.NullPointerException
04-21 21:10:20.678: E/AndroidRuntime(1100):     at com.example.helloworld.Main.onCreate(Main.java:33)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.Activity.performCreate(Activity.java:5231)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-21 21:10:20.678: E/AndroidRuntime(1100):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-21 21:10:20.678: E/AndroidRuntime(1100):     ... 11 more

2 个答案:

答案 0 :(得分:0)

getText()返回Editable个对象。您必须将其转换为字符串:

nameview.setText("Hello "+namefield.getText().toString());

答案 1 :(得分:0)

更改此行

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

我认为,这有助于......