moustafa.kass moustafa.kass - 4 months ago 13
Java Question

App doesn't start, it is instantly force stopped

I have been searching in my code for errors, but couldn't find any. Please help me, I need help.
Note: I am a beginner in android and in android studio.

the problem is that there is no errors shown in android studio, but still i can't open the application on the phone.

This is my code



main.java

import android.support.v7.app.AppCompatActivity;



import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
Button h,s,exit;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h= (Button) findViewById(R.id.h);
t=(TextView) findViewById(R.id.t);
h.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setVisibility(v.INVISIBLE);
s.setVisibility(v.VISIBLE);
h.setVisibility(v.INVISIBLE);
}
});
s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setVisibility(v.VISIBLE);
s.setVisibility(v.INVISIBLE);
h.setVisibility(v.VISIBLE);
}
});
exit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});

}
}


the layout main.xml

<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=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/t"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide"
android:id="@+id/h"
android:layout_below="@+id/t"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
android:id="@+id/s"
android:layout_below="@+id/t"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"
android:id="@+id/e"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Answer

In your code you are using

s.setVisibility(v.VISIBLE);  

But you did not initialize it in your MainActivity. Use:

s = (Button) findViewById(R.id.s);  
exit = (Button) findViewById(R.id.e);
Comments