Watching video tutorials on StartAndroid, stuck on 29 (Calling activations and getting the result). The application crashes, does not start, a window appears with the inscription "Unfortunately, OnClick has stoped".

Code for both classes:

public class MainActivity extends AppCompatActivity { TextView tv = (TextView) findViewById(R.id.textView); Button btn = (Button) findViewById(R.id.button); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { Intent intent = new Intent(this, InputActivity.class); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null) { return; } String name = data.getStringExtra("name"); tv.setText("Your name is " + name); } } 

 public class InputActivity extends AppCompatActivity{ EditText et = (EditText) findViewById(R.id.editText); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.input_activity); } public void onClick(View view) { Intent intent = new Intent(); intent.putExtra("name", et.getText().toString()); setResult(RESULT_OK, intent); finish(); } } 

Manifest:

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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=".InputActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

    2 answers 2

    Trying to find the view as you do is a blunder. In your case, the findViewById methods will be called when the class is created. At this stage, the onCreate method, and therefore the setContentView method setContentView has not yet been called, so your fields will always be null . The findViewById method makes sense to call only after the setContentView method. Then the markup will already be installed, and if there are elements in it with the id transmitted, the search method will return them.

    Further, in your manifest, both activites are declared with a filter

     <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 

    android.intent.action.MAIN indicates that this activity is the entry point to the application. An application can have only one entry point.

    android.intent.category.LAUNCHER indicates that the icon for this activation should be placed in the application launcher system.

    These parameters should be assigned to only one activation, and not like you have two.

    • Thank you for your comments. - sherzod.sadiev

    Move the View initialization to the onCreate(); method onCreate();

    Like this:

     @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.input_activity); tv = (TextView) findViewById(R.id.textView); btn = (Button) findViewById(R.id.button); }