Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Other than the answers posted. You are trying to initialize views without setting the content to the activity in NextActivity. You can findviewbyid of the views of the current view hierarchy set to the activity.</p> <p>If you need to navigate from one activity to another say on button click</p> <pre><code> startActivity(new Intent(ActivityName.this,NextActivity.class); or startActiivty(new Intent("packagename.NextActivity); </code></pre> <p>setContentView. You have misunderstood the purpose of setContentView. </p> <p><a href="http://developer.android.com/reference/android/app/Activity.html#setContentView(int)" rel="nofollow">http://developer.android.com/reference/android/app/Activity.html#setContentView(int)</a></p> <p><strong>Set the activity content to an explicit view</strong>. </p> <p>This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.</p> <p>Parameters</p> <pre><code> view The desired content to display. </code></pre> <p>In your NextActivity</p> <pre><code> @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // this should be first not in buttonclick2() textView2 = (TextView) findViewById(R.id.textView2); // then initialize views. ... } </code></pre> <p>Example:</p> <pre><code> public class MainActivity extends Activity { String passw; TextView textView; Button button; EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); editText = (EditText) findViewById(R.id.editText1); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { public void onClick(View p1) { passw = editText.getText().toString(); if (passw.equals("kev")) { Toast.makeText(MainActivity.this, "corrrect " + passw, Toast.LENGTH_SHORT).show(); startActivity(new Intent(MainActivity.this,NextActivity.class)); } else { Toast.makeText(MainActivity.this, "incorrect " + passw, Toast.LENGTH_SHORT).show(); } } }); } } </code></pre> <p>activity_main.xml</p> <pre><code>&lt;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" &gt; &lt;EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="20dp" android:ems="10" android:inputType="textPassword" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignParentTop="true" android:layout_marginLeft="83dp" android:text="Password" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_alignParentBottom="true" android:layout_marginLeft="39dp" android:text="Button" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>NextActivity.java</p> <pre><code>public class NextActivity extends Activity { Button button2; TextView textView2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.next); textView2 = (TextView) findViewById(R.id.textView1); textView2.setText("Welcome Kevin"); button2 = (Button) findViewById(R.id.button1); button2.setOnClickListener(new OnClickListener(){ public void onClick(View p1) { textView2.setText("button clicked"); } }); } } </code></pre> <p>next.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="TextView" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_alignParentBottom="true" android:text="Button" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Manifest.xml</p> <pre><code> &lt;activity android:name="com.example.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="com.example.NextActivity" android:label="@string/app_name" &gt; &lt;/activity&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload