简体   繁体   中英

Launching a new activity just by touching the screen

I am trying to launch a brand new activity that will display a new string when I touch the screen. Here is the MainActivity code. My first activity displays a "hello world", and my new activity should simply displays a new string. All the resource files and the manifest are correct. However, the run keeps crashing after the onClick. Any help is much appreciated.

package com.example.myfirsttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity{

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


    //Call the NewActivity from the MainActivity
        View view = getWindow().getDecorView().findViewById(android.R.id.content);
        view.setOnClickListener(new OnClickListener() {

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

            }
        }); 
    }
}

There is a better way to accomplish this

package com.example.myfirsttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity{
private static final String MESSAGE_TAG = "my_message";

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
    Bundle extras = getIntent().getExtras();
    String stringMessage = extras.getString(MESSAGE_TAG);
    TextView message = (TextView) findViewById(R.id.message);
    if(stringMessage != null && !stringMessage.isEmpty()) {
       message.setText(stringMessage);
    }


    //Call the NewActivity from the MainActivity
        View view = getWindow().getDecorView().findViewById(android.R.id.content);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent startIntent = new Intent(MainActivity.this,MainActivity.class);
                startIntent.putExtra(MESSAGE_TAG, "hello second World");
                startActivity(startIntent);

            }
        }); 
    }
}

Now you do not need to have 2 activities registered for the same task here is the layout

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

    <TextView
        android:id="@+id/message"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

happy coding.

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