Android -Simple随机应用

时间:2017-08-30 17:43:30

标签: java android

我正在尝试构建一个应用程序并且有一些问题。

Android监视器:

08-31 07:41:59.705 10118-10118/com.example.user.rotateit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.rotateit/com.example.user.rotateit.HasilActivity}: java.lang.NullPointerException
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                                               at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                               at android.os.Looper.loop(Looper.java:137)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.NullPointerException
                                                                               at com.example.user.rotateit.HasilActivity.onCreate(HasilActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:5008)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                                               at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                               at android.os.Looper.loop(Looper.java:137) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 

HasilActivity.Java:

public class HasilActivity extends MainActivity {

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

        int choice = 0;
        final Random random = new Random();
        choice = random.nextInt(2)+0;


        EditText et_black = (EditText) findViewById(R.id.et_black);
        EditText et_white = (EditText) findViewById(R.id.et_white);
        TextView tv_result = (TextView) findViewById(R.id.tv_result);
        
        String et_blackText = et_black.getText().toString();
        String et_whiteText = et_white.getText().toString();


        if (choice == 0) {
            tv_result.setText(et_blackText);
        }
        if (choice == 1) {
            tv_result.setText(et_whiteText);
        }
    }
}

XML(activity_hasil):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.rotateit.HasilActivity">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/tv_result"/>


</LinearLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity {

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




        ImageView btn_start = (ImageView) findViewById(R.id.btn_start);

        btn_start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, HasilActivity.class);
                startActivity(i);
            }
        });

    }
}

XML(activity_main):

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#42079D"
    tools:context="com.example.user.rotateit.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:id="@+id/et_black"
            style="@style/EditText1"
            android:background="#0B0503"
            android:textColor="#FEFCFB"

            />

        <EditText
            android:id="@+id/et_white"
            style="@style/EditText1"
            android:background="#FEFCFB"
            android:textColor="#0B0503"

            />


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_start"
            android:id="@+id/btn_start"
            android:layout_gravity="center"/>
    </LinearLayout>
</ScrollView>

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.rotateit">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

运行程序时,应用程序始终停止工作。 这是整个计划。我很确定问题出现在editText中。

基本上,我希望程序本身能够随机显示用户输入的内容。因此,有两个EditTexts,et_black和et_white。用户将输入editText中的任何文本,而程序将显示et_black或et_white。

刚开始学习android studio。

2 个答案:

答案 0 :(得分:0)

您必须使用et_black String对象中的文本创建TextEdit。之后,您可以将此字符串分配给tv_result TextView

String et_blackText = et_black.getText().toString();

if (choice == 0 || choice == 1) {
    tv_result.setText(et_blackText);
}

此外,您必须在XML中创建以下对象

EditText et_black = (EditText) findViewById(R.id.et_black);
EditText et_white = (EditText) findViewById(R.id.et_white);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
您的HasilXML文件中未创建

R.id.et_blackR.id.et_whiteR.id.tv_result

答案 1 :(得分:0)

您正在尝试将EditText et_black作为TextView的文本。 我不会说别的,只是......仔细查看你的代码!!

PS发布manifest.xml