KVK KVK - 7 months ago 13
Java Question

Link two layouts in android

I am trying to link my two Android layout and make the process.

MainActivity.java

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});



}

public void open(View view){
AlertDialog.Builder alb=new AlertDialog.Builder(this);
alb.setMessage("Are You Sure Do You Want This");
alb.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"You Clicked Yes Button",Toast.LENGTH_LONG).show();
}
});

alb.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"You Clicked Yes Button",Toast.LENGTH_LONG).show();
//finish();
}
});
AlertDialog al=alb.create();
al.show();
}


public void clockwise(View view){
ImageView iv=(ImageView) findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.myanimation);
iv.startAnimation(animation);
}
public void zoom(View view){
ImageView iv=(ImageView) findViewById(R.id.imageView);
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.clockwise);
iv.startAnimation(animation);
}

public void fade(View view){
ImageView iv=(ImageView) findViewById(R.id.imageView);
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);
iv.startAnimation(animation);
}

public void blink(View view){
ImageView iv=(ImageView)findViewById(R.id.imageView);
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.blink);
iv.startAnimation(animation);
}

public void move(View view){
ImageView iv=(ImageView)findViewById(R.id.imageView);
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
iv.startAnimation(animation);
}

public void slide(View view){
ImageView iv=(ImageView)findViewById(R.id.imageView);
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
iv.startAnimation(animation);
}






public void next(View v){
// setContentView(R.layout.recording);
Intent inent = new Intent(this, RecordActivity.class);

startActivity(inent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


RecordActivity.java

public class RecordActivity extends Activity {
private MediaRecorder mr;
Button play,record,stop;
private String outputFile=null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recording);

play=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.button2);
record=(Button)findViewById(R.id.button);
stop.setEnabled(false);
play.setEnabled(false);

outputFile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";
mr=new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mr.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mr.setOutputFile(outputFile);

}


public void Record(View v){
try{
mr.prepare();
mr.start();
}
catch (Exception e){
e.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);


}


public void stop(View v){
mr.stop();
mr.release();
mr=null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(),"AudioRecorded Sucessfully",Toast.LENGTH_LONG);

}

public void play(View v){
MediaPlayer mp=new MediaPlayer();
try{
mp.setDataSource(outputFile);
mp.prepare();
}
catch (Exception e){
e.printStackTrace();
}
mp.start();
Toast.makeText(getApplicationContext(),"Playing Audio",Toast.LENGTH_LONG);
}

}


content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.admin.alertapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog"
android:id="@+id/textView"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raptor"
android:id="@+id/textView2"
android:textColor="#da644860"
android:textSize="35dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zoom"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp"
android:onClick="clockwise"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clockwise"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true"
android:onClick="zoom"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fade"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="fade"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blink"
android:onClick="blink"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="move"
android:onClick="move"
android:id="@+id/button5"
android:layout_below="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:onClick="slide"
android:id="@+id/button6"
android:layout_below="@+id/button3"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="next"
android:id="@+id/button61"
android:layout_below="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />

</RelativeLayout>


recording.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 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"
android:weightSum="1">

<TextView
android:layout_width="234dp"
android:layout_height="112dp"
android:text="Android Audio Recording"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"

android:textSize="30dp"
android:textColor="#d50c0c"
android:layout_weight="0.01" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="86dp"
android:id="@+id/imageView2"
android:src="@drawable/logo"
android:layout_gravity="center_horizontal"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/button7"
android:layout_below="@+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="59dp"
android:onClick="Record"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/button8"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:onClick="stop"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play"
android:id="@+id/button9"
android:layout_alignTop="@+id/button8"
android:layout_alignRight="@+id/textView3"
android:layout_alignEnd="@+id/textView3"
android:layout_gravity="right"
android:onClick="play"
/>

</LinearLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.alertapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity
android:name=".RecordActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>


</application>

</manifest>



stack trace
04-22 14:24:09.415 4470-4470/com.example.admin.alertapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.alertapplication, PID: 4470
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.alertapplication/com.example.admin.alertapplication.RecordActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at com.example.admin.alertapplication.RecordActivity.onCreate(RecordActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 


I am using the above code to for linking the two layouts when next button was clicked next() function will be called after that Unfortunately application has stopped why it's happening can any one help me to fix this

Answer

The reason is in RecordActivity in this line
play=(Button)findViewById(R.id.button3);
Because recording.xml doesn't contain button3 so play(Button) will be null
Then you call play.setEnabled(false); => your app crash by NullPointerException