Reputation: 47
This is my activity_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: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" >
<ImageButton
android:id="@+id/clicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:onClick="increment"
android:src="@drawable/crown"
/>
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/clicker"
android:text="0" />
</RelativeLayout>
This is MainActivity.java:
package com.example.ProjectName;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView output = (TextView) findViewById(R.id.output);
int clickCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increment() {
clickCount++;
output.setText(clickCount);
}
@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;
}
}
This application is supposed to have a button that when you click it, a textview underneath the button displays an integer which increments with each click.
However, when I run this on my AVD it just says "ProjectName has stopped working". The IDE is giving me no errors. I tried previously instead of using "onClick:" in the xml manifest I st onClickListeners in the onCreate method but I got the same errors.
I see no reason why this should error, I barely entered any code besides what is created by default. I am an inexperienced android programmer and this is getting frustrating. Thanks for your help.
Edit for Binghammer:
MainActivity.java:
public class MainActivity extends Activity {
TextView output;
int clickCount = 0;
Button clicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.output);
clicker = (Button) findViewById(R.id.clicker);
clicker.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
clickCount++;
output.setText(clickCount);
}
});
}
@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:
<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" >
<ImageButton
android:id="@+id/clicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:src="@drawable/crown" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/clicker"
android:text="0" />
</RelativeLayout>
With the code for an onClickListener on Button clicker, app doesn't start.
Upvotes: 0
Views: 487
Reputation: 1850
Try
public class MainActivity extends Activity {
TextView output;
int clickCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.output);
}
For your 2nd problem, try
public void increment(View view) {
output = (TextView) findViewById(R.id.output);
clickCount++;
output.setText("" + clickCount);
}
Use the one without onclicklistener and see if it works
Upvotes: 4
Reputation: 13483
In your original class, you defined the TextView
before it was created in onCreate
, so it crashed.
In your edited class, this could be the problem:
output.setText(clickCount);
may need to be:
output.setText("" + clickCount);
Upvotes: 1