简体   繁体   中英

Having trouble creating an android app

I'm trying to make an app that you type in your name it prints out hello whatever the name your name is but every time I try to open the app either the app on wither my phone or an emulator it says unfortunately helloworld (my original app name) has stopped. This is my main code; any help is appreciated

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>

Log cat

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

getText() returns and Editable object. You have to convert it to a string:

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

change this line

setContentView(R.layout.activity_main);

to

setContentView(R.layout.fragment_main);

I think, that helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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