Android crash in eclipse android SDK - java

I'm beginner in android developement. And i have a problem with my code. The application is totally crashed. It is Java error in main.
MainActivity.java:
package hu.cgkni6.uniobuda.testapplication;
import hu.cgkni6.uniobuda.testapplication.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
final String username = "diak";
final String pass = "diak";
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (usrname.getText().toString().equals(username))
{
if (passwd.getText().toString().equals(pass))
{
String text2 = "Success!";
Toast.makeText(MainActivity.this, text2, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Invalid Password", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Invalid username", Toast.LENGTH_LONG).show();
}
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#45B7FA" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:text="#string/login_interface" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/username" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/password" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_OK"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/ok" />
<Button
android:id="#+id/btn_Cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/cancel" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/about_us"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/contact_us" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/help" />
</TableRow>
</RelativeLayout>
</LinearLayout>
Catlog:
10-09 09:18:26.244: E/AndroidRuntime(302): FATAL EXCEPTION: main
10-09 09:18:26.244: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.cgkni6.uniobuda.testapplication/hu.cgkni6.uniobuda.testapplication.MainActivity}: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 09:18:26.244: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Activity.findViewById(Activity.java:1637)
10-09 09:18:26.244: E/AndroidRuntime(302): at hu.cgkni6.uniobuda.testapplication.MainActivity.<init>(MainActivity.java:17)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstanceImpl(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstance(Class.java:1429)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-09 09:18:26.244: E/AndroidRuntime(302): ... 11 more

Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
Put this in the oncreate method after setContentView(R.layout.activity_main);
and you should be good. You cant do it from that location :)

You should initialize your Views after setting the layout in your onCreate() method.
You are getting the error because you are trying to access the views before setting the layout that is why the application is not able to find the view's id.
Change your code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}

You're trying to find views before your layout is setup. Move
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
to your onCreate method. Just after your setContentView call.

First of all you should learn Android life cycle
you should find your all button and edit text in the oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}

Initialize you Button and EditText outside the onCreate method like this
Button okbutton;
Button cancelbutton;
EditText usrname;
EditText passwd;
Then insert this
okbutton = (Button) findViewById(R.id.btn_OK);
cancelbutton = (Button) findViewById(R.id.btn_Cancel);
EditText usrname = (EditText) findViewById(R.id.editText1);
EditText passwd = (EditText) findViewById(R.id.editText2);
inside your onCreate method after the setContentView(R.layout.activity_main);
Hope it helps.

Related

Facing Null Point exception in declaring button and spinner - Android

Hi am trying to create a simple application using button and spinners. Here is my code. i am facing a nullpointexception error.
Code
package baman.lankahomes.lk.jaffnatemples;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
public final Button btn_search = (Button) findViewById(R.id.btn_search);
public final Spinner spn_from = (Spinner) findViewById(R.id.spinner_from);
public final Spinner spn_radius = (Spinner) findViewById(R.id.spinner_radius);
public final Spinner spn_temple_type = (Spinner) findViewById(R.id.spinner_temple_type);
public final Spinner Spn_no_temple = (Spinner) findViewById(R.id.spinner_no_of_temples);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
validate_form();
goToNextActivity();
}
});
}
public void goToNextActivity(){
Intent intent = new Intent(getApplicationContext(), SearchResult.class);
startActivity(intent);
}
public int validate_form(){
String from = spn_from.getSelectedItem().toString();
if(from == "Please Select"){
show_error_message("Error!", "Please select the from location");
}
return 1;
}
private void show_error_message(String data, String title){
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(data)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
Layout XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/search_from"
android:id="#+id/txt_from"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:clickable="false"
android:textColor="#ffffff"
android:textSize="18sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_from"
android:layout_below="#+id/txt_from"
android:entries="#array/from_spinnerItems"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/distance_radius"
android:id="#+id/tv_distance"
android:textStyle="bold"
android:clickable="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_below="#+id/spinner_from"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_radius"
android:layout_below="#+id/tv_distance"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:entries="#array/distanceRadiusItems"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/temple_type"
android:id="#+id/tv_temple_type"
android:textStyle="bold"
android:clickable="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_below="#+id/spinner_radius"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_temple_type"
android:layout_below="#+id/tv_temple_type"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:entries="#array/templeTypeItems"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/nooftemples"
android:id="#+id/tv_nooftemples"
android:textStyle="bold"
android:clickable="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_below="#+id/spinner_temple_type"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_no_of_temples"
android:layout_below="#+id/tv_nooftemples"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:entries="#array/NoOfTemplesItems"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search"
android:id="#+id/btn_search"
android:layout_below="#+id/spinner_no_of_temples"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:background="#drawable/button_selector"
android:textColor="#android:color/white"
android:textStyle="bold"
android:textSize="18dp" />
</RelativeLayout>
Error
FATAL EXCEPTION: main
Process: baman.lankahomes.lk.jaffnatemples, PID: 6424
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{baman.lankahomes.lk.jaffnatemples/baman.lankahomes.lk.jaffnatemples.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1954)
at baman.lankahomes.lk.jaffnatemples.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378) 
at android.app.ActivityThread.access$800(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5433) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515)
public final Button btn_search = (Button) findViewById(R.id.btn_search);
public final Spinner spn_from = (Spinner) findViewById(R.id.spinner_from);
public final Spinner spn_radius = (Spinner) findViewById(R.id.spinner_radius);
public final Spinner spn_temple_type = (Spinner) findViewById(R.id.spinner_temple_type);
public final Spinner Spn_no_temple = (Spinner) findViewById(R.id.spinner_no_of_temples);
needs to be inside onCreate() after setContentView()..that is how your activity will know about your components avoiding the null pointer exception.
Move
Button btn_search = (Button) findViewById(R.id.btn_search);
under onCreate(....) and after setContentView(R.layout.activity_main);
do for others too
Correct:
public class MainActivity extends AppCompatActivity {
public Button btn_search;
public Spinner spn_from;
public Spinner spn_radius;
public Spinner spn_temple_type;
public Spinner Spn_no_temple;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_search = (Button) findViewById(R.id.btn_search);
spn_from = (Spinner) findViewById(R.id.spinner_from);
spn_radius = (Spinner) findViewById(R.id.spinner_radius);
spn_temple_type = (Spinner) findViewById(R.id.spinner_temple_type);
Spn_no_temple = (Spinner) findViewById(R.id.spinner_no_of_temples);
btn_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
validate_form();
goToNextActivity();
}
});
}
Move
public final Button btn_search = (Button) findViewById(R.id.btn_search);
public final Spinner spn_from = (Spinner) findViewById(R.id.spinner_from);
public final Spinner spn_radius = (Spinner) findViewById(R.id.spinner_radius);
public final Spinner spn_temple_type = (Spinner) findViewById(R.id.spinner_temple_type);
public final Spinner Spn_no_temple = (Spinner) findViewById(R.id.spinner_no_of_temples);
to onCreate() after setContentView(R.layout.activity_main);

How to make edittext value in activity A appear in a listview in activity B?

I get a nullpointer error at smashlist.setAdapter(Lincadapter); in Activity B.
I made sure the id's were all correct so the problem isn't there. What am I doing wrong and how do I fix this?
Activity A -->
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8), (int)(height*.7));
}
public void enter(View view){
Intent i = new Intent(Pop.this, MainActivity.class);
final EditText name = (EditText)findViewById(R.id.name);
final EditText age = (EditText)findViewById(R.id.age);
final EditText ethnicity = (EditText)findViewById(R.id.ethnicity);
final EditText hair = (EditText)findViewById(R.id.hair);
final EditText date = (EditText)findViewById(R.id.date);
final EditText extras = (EditText)findViewById(R.id.extras);
String nameinput = name.getText().toString();
String ageinput = age.getText().toString();
String ethnicityinput = ethnicity.getText().toString();
String hairinput = hair.getText().toString();
String dateinput = date.getText().toString();
String extrasinput = extras.getText().toString();
i.putExtra("name", nameinput);
i.putExtra("age", ageinput);
i.putExtra("ethnicity", ethnicityinput);
i.putExtra("hair", hairinput);
i.putExtra("date", dateinput);
i.putExtra("extras", extrasinput);
startActivity(i);
};
}
Activity A XML -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="words"
android:layout_weight="1"
android:id="#+id/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:maxLength="2"
android:layout_weight="1"
android:id="#+id/age"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ethnicity"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="words"
android:layout_weight="1"
android:id="#+id/ethnicity"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hair color"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="sentences"
android:layout_weight="1"
android:id="#+id/hair"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:hint="Date"
android:maxLines="1"
android:maxLength="10"
android:singleLine="true"
android:layout_weight="1"
android:id="#+id/date"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Extras"
android:maxLines="3"
android:maxLength="200"
android:capitalize="sentences"
android:layout_weight="1"
android:id="#+id/extras"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/tickbutton"
android:clickable="true"
android:layout_gravity="right"
android:layout_weight="1"
android:onClick="enter"/>
Activity B-->
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Bundle smashdata = getIntent().getExtras();
if (smashdata == null){
return;
}
String nameinput = smashdata.getString("name");
String ageinput = smashdata.getString("age");
String ethnicityinput = smashdata.getString("ethnicity");
String hairinput = smashdata.getString("hair");
String dateinput = smashdata.getString("date");
String extrasinput = smashdata.getString("extras");
final ListView smashlist = (ListView) findViewById(R.id.smashlist);
String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};
ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, info);
smashlist.setAdapter(lincadapter);
smashlist.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
);
Activity B XML -->
<TextView android:text="I've Smashed 0 Girls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:textColor="#000000"
/>
<ListView
android:id="#+id/smashlist"
android:layout_width="wrap_content"
android:layout_height="272dp"
android:layout_weight="1.00"
android:background="#ff0000"/>
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:clickable="true"
android:src="#drawable/plusbutton"
android:layout_gravity="center_horizontal"
android:onClick="add"/>
Crash log --->
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kongapps.smashlog.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
I think the error is this:
Activity A
i.putExtra("date", dateinput);
and then
Activity B
String dateinput = smashdata.getString("datename");
you call differently to that variable.
My advice would be to use static constants to be free from such errors . :)
Regards.
it seems your string array has problem, check if the string's inside actually have some value of not other wise you will get Null error
String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};
for now write your info array like this to verify it works
String[] info = new String[]{"nameinput", "ageinput", "ethnicityinput", "hairinput", "dateinput", "extrasinput"};
Create a static ArrayList in A activity add all values in pass this array in ListView adapter
Example:
ArrayList<String>_array = new ArrayList<>();
_array.add(nameinput);
In B Activity
ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, A._array)
Write
String [] info= new String[6];
And then add the values in info={your values};
This might solve your null pointer exception

Android Studio Program Crash

I have two activities Main, and Game_Activity
When my app launches and I click a button to take me to the Game_Activity it crashes...
Main
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHelp = (Button) findViewById(R.id.btnHelp);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnStart.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, GameActivity.class); //gets us to Game activity when Start Button is clicked
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
});
}
Game_Activity
ViewGroup kamilsLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
Button kamilsButton2 = (Button) findViewById(R.id.kamilsButton2);
kamilsButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
moveButton();
}
});
}
public void moveButton(){
View kamilsButton = findViewById(R.id.kamilsButton);
TransitionManager.beginDelayedTransition(kamilsLayout);
//Change size of the button
ViewGroup.LayoutParams sizeRules = kamilsButton.getLayoutParams();
sizeRules.height = 700;
kamilsButton.setLayoutParams(sizeRules);
}
Here is Logcat sorry for last lines but couldn't format it... :/
03-02 21:32:49.073 22427-22427/com.example.kamil.mychemicalreaction E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kamil.mychemicalreaction, PID: 22427
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamil.mychemicalreaction/com.example.kamil.mychemicalreaction.GameActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kamil.mychemicalreaction.GameActivity.onCreate(GameActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)at android.app.ActivityThread.access$900(ActivityThread.java:161)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:126)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:157)at android.app.ActivityThread.main(ActivityThread.java:5356)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)at dalvik.system.NativeStart.main(Native Method)
XML
Game_Activity
<Button
android:layout_width="150dp"
android:layout_height="1dp"
android:id="#+id/kamilsButton"
android:gravity="bottom|fill_vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff6aff01" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Click"
android:id="#+id/kamilsButton2"
android:textSize="9dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp" />
Menu
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Chemical Reaction"
android:id="#+id/txtTitle"
android:textStyle="bold"
android:textColor="#ffa946ff"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btnStart"
android:text="Start"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginBottom="61dp"
android:layout_above="#+id/btnSettings"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings" />
<Button
android:id="#+id/btnSettings"
android:text="Settings"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btnHelp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp" />
<Button
android:id="#+id/btnHelp"
android:text="Help"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings"
android:layout_marginBottom="81dp" />
You're getting a NullPointerException when trying to reference your UI elements. From your XML, it appears you haven't given any layout an id of kamilsLayout. Try enclosing your UI elements in a layout with this ID.
Like Josef said. You're setting the same XML for two both activities, is that expected?
You're using setContentView(R.layout.activity_main). Maybe there is no ViewGroup on R.layout.activity_main layout. And you're trying to setting up that `
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
`

I get Fatal Exception using multiple OnClickListeners

The Main thing happens in FATAL EXECEPTION my App don't Launches and error comes unfortunately stopped it keeps crashing
Tried switch case and else if still the error continues the app keeps crashing
Googled and search over stackflow the different usersover stackflowed faced this error still the error continues with me even after trying their solution with single if and single onclicklistener app manages to run but not with multiple onlicklistener is there a different way to run multiple onclick listener app please help me
here's MainActivity.java
package com.example.cricscore;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private int mCount = 0;
private static final String TAG = "MainActivity";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) this);
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.button1:
mCount++;
final TextView countTextView = (TextView) findViewById(R.id.textView1);
countTextView.setText(""+ mCount);
break;
case R.id.button2:
mCount= mCount+2 ;
final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
countTextView1.setText(""+ mCount);
break;
}
}
catch(Throwable t) {
Log.e(TAG, t.getMessage(), t);
}
}
}
And the Activity_main.xml is
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/textView1"
android:text="Runs" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/textView2"
android:layout_marginRight="19dp"
android:text="Wickets" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView4"
android:layout_alignParentRight="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text="1" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="2" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:text="6" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button5"
android:layout_alignRight="#+id/textView4"
android:text="Wicket" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button8"
android:layout_alignBottom="#+id/button8"
android:layout_alignLeft="#+id/button5"
android:text="Balls" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_toRightOf="#+id/button7"
android:text="4" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="44dp"
android:layout_toLeftOf="#+id/textView2"
android:text="Button" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:layout_alignBottom="#+id/button4"
android:layout_toLeftOf="#+id/textView7"
android:text="No/Wide" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView7"
android:layout_alignBottom="#+id/textView7"
android:layout_alignRight="#+id/button5"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button8"
android:layout_toLeftOf="#+id/textView4"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</RelativeLayout>
Logcat
03-18 14:09:37.717: E/AndroidRuntime(360): FATAL EXCEPTION: main
03-18 14:09:37.717: E/AndroidRuntime(360): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.abc/com.example.abc.MainActivity}: java.lang.ClassCastException:
com.example.abc.MainActivity
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1663)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread.access$1500
(ActivityThread.java:117)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:931)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.os.Handler.dispatchMessage
(Handler.java:99)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.os.Looper.loop(Looper.java:123)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread.main
(ActivityThread.java:3683)
03-18 14:09:37.717: E/AndroidRuntime(360): at java.lang.reflect.Method.invokeNative(Native
Method)
03-18 14:09:37.717: E/AndroidRuntime(360): at java.lang.reflect.Method.invoke
(Method.java:507)
03-18 14:09:37.717: E/AndroidRuntime(360): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 14:09:37.717: E/AndroidRuntime(360): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:597)
03-18 14:09:37.717: E/AndroidRuntime(360): at dalvik.system.NativeStart.main(Native Method)
03-18 14:09:37.717: E/AndroidRuntime(360): Caused by: java.lang.ClassCastException:
com.example.abc.MainActivity
03-18 14:09:37.717: E/AndroidRuntime(360): at com.example.abc.MainActivity.onCreate
(MainActivity.java:30)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-18 14:09:37.717: E/AndroidRuntime(360): ... 11 more
After Some Modification As told by the 1st answer my code didn't crashed but I was only able to see blank activity where android_mainfest is still the same
package com.example.cricscore;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
// Private member field to keep track of the count
private int mCount = 0;
/** Called when the activity is first created.
* #param OnClickListener */
public void onCreate(Bundle savedInstanceState, android.view.View.OnClickListener
OnClickListener){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) );
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) );
}
public void OnClickListener(View v) {
switch (v.getId()) {
case R.id.button1:
mCount++;
final TextView countTextView = (TextView) findViewById(R.id.textView1);
countTextView.setText(""+ mCount);
break;
case R.id.button2:
mCount= mCount+2 ;
final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
countTextView1.setText(""+ mCount);
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The setOnClickListener method expects a parameter of type View.OnClickListener. What you are doing is taking the Activity itself, and trying to coerce it being a OnClickListener which it is not, in this piece of code:
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) this); // <-- here
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) this); // <-- and here too
You should declare your activity to be an OnClickListener, using:
public class MainActivity extends Activity implements OnClickListener
and as you already have a onClick(View v) method, the problem should be fixed.

"unable to start activity component info" droid force close issue

Here's the java code for the class
package com.warriorsoft.mtgupanddown;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
int guy1;
int guy2;
MediaPlayer sound;
Button plus1g1, plus1g2, min1g1, min1g2, plus5g1, plus5g2, min5g1, min5g2,
GG, RESET;
TextView g1, g2;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
guy1 = 20;
guy2 = 20;
min5g1 = (Button) findViewById(R.id.button1); // x
plus5g1 = (Button) findViewById(R.id.button2); //x
plus1g1 = (Button) findViewById(R.id.button3); // x
min1g1 = (Button) findViewById(R.id.button4); // x
min5g2 = (Button) findViewById(R.id.button5); // x
plus5g2 = (Button) findViewById(R.id.button6); //x
min1g2 = (Button) findViewById(R.id.button7); // x
plus1g2 = (Button) findViewById(R.id.button8); //x
RESET = (Button) findViewById(R.id.RESET);
GG = (Button) findViewById(R.id.GG);
g1 = (TextView) findViewById(R.id.tvPlayer1HP);
g2 = (TextView) findViewById(R.id.tvPlayer2HP);
min5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
guy1 -= 5;
g1.setText(guy1);
}
});
min5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 5;
g2.setText(guy2);
}
});
min1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 -= 1;
g1.setText(guy1);
}
});
min1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 1;
g2.setText(guy2);
}
});
plus5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 5;
g2.setText(guy2);
}
});
plus5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 5;
g1.setText(guy1);
}
});
plus1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 1;
g1.setText(guy1);
}
});
plus1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 1;
g2.setText(guy2);
}
});
RESET.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
guy1 = 20;
guy2 = 20;
g2.setText(guy2);
g1.setText(guy1);
}
});
GG.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
sound = MediaPlayer.create(MainActivity.this, R.raw.gg );
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Here's the layout xml file
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tvPlayer2HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="36dp"
android:text="20"
android:textSize="35dp" />
<TextView
android:id="#+id/tvPlayer1HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:text="20"
android:textSize="35dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/button1"
android:text="+5" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:text="-1" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/button1"
android:layout_marginBottom="28dp"
android:text="+1" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignParentRight="true"
android:text="+5" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/button6"
android:text="-5" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button5"
android:layout_alignTop="#+id/button4"
android:text="+1" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_toRightOf="#+id/button5"
android:text="-1" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/button5"
android:ems="10"
android:hint="Guy 1" >
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button1"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Guy 2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvPlayer1HP"
android:layout_marginTop="74dp"
android:text="-5" />
<ImageView
android:id="#+id/GG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvPlayer2HP"
android:layout_centerHorizontal="true"
android:minHeight="140dp"
android:minWidth="120dp"
android:src="#drawable/gg" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Optimal sideways"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/RESET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="RESET" />
</RelativeLayout>
And here's the logcat
01-16 14:01:51.292: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-16 14:01:51.302: E/AndroidRuntime(699): FATAL EXCEPTION: main
01-16 14:01:51.302: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.warriorsoft.mtgupanddown/com.warriorsoft.mtgupanddown.MainActivity}: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:123)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 14:01:51.302: E/AndroidRuntime(699): at dalvik.system.NativeStart.main(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at com.warriorsoft.mtgupanddown.MainActivity.onCreate(MainActivity.java:33)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-16 14:01:51.302: E/AndroidRuntime(699): ... 11 more
01-16 14:01:53.712: I/Process(699): Sending signal. PID: 699 SIG: 9
The problem is running the code on a phone.
It tells me that the program broke in some unexpected way and forces me to force close.
If anyone could please help me understand what i've done wrong i'll be eternally grateful
I am still new to programming and can barely understand the logcats
please go easy
Thankyou for trying
You are finding the ImageView and casting it to Button.
check this line of code.
GG = (Button) findViewById(R.id.GG);
Change the type of GG as ImageView and typecast to ImageView as below:
ImageView GG;
GG = (ImageView) findViewById(R.id.GG);
Change this to this
GG = (Button) findViewById(R.id.GG);
to
GG = (ImageView) findViewById(R.id.GG);
you cannot cast an ImageView to a Button. Which is what you are doing

Categories

Resources