RuntimeError for activity specified on Android Manifest - java

I have this problem that just seems strange.
I have this Android View with six buttons and when i click on one of them(the only one with the action defined apart from the exit button), it gives me a RuntimeError.
The Activity is declared on the manifest, and this is why i don't understand my error.
Can someone please help me with this.
I would really appreciate it :)
So here's my code:
package com.example.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}
#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;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_create:
Intent i = new Intent(this, CreateAppointment.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
My layout is like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="225dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<CalendarView
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="2dip"
android:layout_marginTop="2dip"
android:stretchColumns="*" >
<TableRow>
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
<Button
android:id="#+id/btn_viewEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewEdit" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/delete" />
<Button
android:id="#+id/btn_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/move" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search" />
<Button
android:id="#+id/btn_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/translate" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/exit"/>
</LinearLayout>
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calendar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.calendar.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateAppointment"
android:theme="#android:style/Theme.Dialog"
android:label="#string/create" >
</activity>
</application>
</manifest>

You have to define correctly what kind of views ares using in onCreate()
View createButton = findViewById(R.id.btn_create);
for example:
since you are using a Button:
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
Make the cast as button:
Button createButton = (Button)findViewById(R.id.btn_create);
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createButton = (Button)findViewById(R.id.btn_create);
Button deleteButton = (Button)findViewById(R.id.btn_delete);
Button moveButton = (Button)findViewById(R.id.btn_move);
Button searchButton = (Button)findViewById(R.id.btn_search);
Button translateButton = (Button)findViewById(R.id.btn_translate);
Button exitButton = (Button)findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}

Related

Creating a back button with noActionBar

I'm a couple days into learning Android development and I'll admit my code is not pretty, as I have a few weeks of Java under my belt on top of this. I'm trying to create a back button on my 2nd main activity page to get back to the 1st main activity, but the only solutions I've come across are while using the toolbar. How would you go about adding this function while using noActionBar?
Main Activity 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:background="#drawable/clearwater"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:background="#drawable/btn"
android:fontFamily="cursive"
android:text="DIVE"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Main Activity 2 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:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/Button2"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:background="#drawable/timebutton"
android:fontFamily="serif-monospace"
android:text="Time Interval Between Dives"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButton"
style="#android:style/Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="11dp"
android:adjustViewBounds="false"
android:background="#android:color/background_light"
android:cropToPadding="false"
android:tint="#android:color/background_dark"
app:srcCompat="?android:attr/actionModeCloseDrawable" />
</RelativeLayout>
Main Activity Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button but1;
public void init(){
but1= (Button)findViewById(R.id.button);
but1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent toy = new
Intent(MainActivity.this,activity_main_2.class);
startActivity(toy);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();{
}
}
}
Main Activity 2 Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ViewAnimator;
public class activity_main_2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goodvibes.helloworld">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".activity_main_2"></activity>
</application>
</manifest>
I had a lot of great people giving me answers I had to research. In the end here is what solved my problem: I inserted "android:onClick="ImageButton" under my button code in the xml fine THEN under the corresponding java file for my second activity I inserted:
public void ImageButton(View v)
{
// some code
finish();
}
Add this to your layout file:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_acashmemoreport"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarColoredBackArrow" />
In your activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_acashmemoreport);
setSupportActionBar(toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
First of all you have to add a required toolbar and after making the toolbar you put the button inside the toolbar like following;
Update your xml layout file as,
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<ImageButton
android:id="#+id/btn_back_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"
/>
</android.support.v7.widget.Toolbar>
Also include the below java code inside your Activity Class,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageButton back=findViewById(R.id.btn_back_toolbar);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,AnotherActivity.class));
}
});

Android Studio: FATAL EXCEPTION: main while getting user inputs

I was trying to use user input name for later use but Once i run the app this is closed by saying unfortunately your app has stopped. I have provided Main activity,Layout xml and android manifest codes. Please anyone answer this . I am newbie in android and cant figure out the error.
Main_activity
package rupakthapa.droiddynasty.com.interactivestory2;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.R.attr.duration;
import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {
private EditText mNameField;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameField);
mButton = (Button) findViewById(R.id.startButton);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String a= mNameField.getText().toString();
Context context = getApplicationContext();
int length = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, a, length);
toast.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
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="rupakthapa.droiddynasty.com.interactivestory2.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="149dp"
android:contentDescription="startImage"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<Button
android:text="Start your adventure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/startButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="rupakthapa.droiddynasty.com.interactivestory2"
xmlns:android="http://schemas.android.com/apk/res/android">
<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:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
There is a problem here. You dont have EditText in your layout.
mNameField = (EditText) findViewById(R.id.nameField);
In your layout you should change TextView to EditText
<EditText
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>

How do i change activity on a button click android

I have an ImageButton that when I want it to be pressed to launch a second Activity. The second activity is called "Dust 2" as seen in my android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pete.smokesapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".homepage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Dust2"
android:label="#string/title_activity_dust2" >
</activity>
</application>
</manifest>
I tried following a guide online but got me as far as this stage now my app crashed on launch.
package pete.smokesapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class homepage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this);
}
public void imgDust2(View view) {
switch (view.getId()){
case R.id.imgDust2:
Toast.makeText(this, "Dust 2 Clicked", Toast.LENGTH_LONG ).show();
break;
}
#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_homepage, 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);
}
Here is also my main activity, this is the activity with the ImageButton, i am also new to this so sorry if I seem clueless.
<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">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Maps"
android:id="#+id/txtMaps"
android:layout_weight="0.44" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgDust2"
android:clickable="false"
android:src="#drawable/dust2"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgInferno"
android:layout_below="#+id/txtOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp"
android:src="#drawable/inferno" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgOverpass"
android:src="#drawable/overpass"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgMirage"
android:clickable="false"
android:src="#drawable/mirage"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgCache"
android:clickable="false"
android:src="#drawable/cache"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgNuke"
android:clickable="false"
android:src="#drawable/nuke"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
In your first activity do this:
Button button = (Button) findViewById(R.id.imgDust2);
button.setOnClickListener(new View.OnClickListener () {
#Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), dust2.class);
startActivity(intent);
}
});
Try the following:
Add implements OnClickListener to your class definition: public class homepage extends ActionBarActivity implements OnClickListener
An error will appear and ask you to add unimplemented methods. Do so.
Change findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this); to findViewById(R.id.imgDust2).setOnClickListener(this);
Place the stuff you want to do when the button is clicked into the new method that was added in step 2.
Set onClick to the xml imageView and put the name of the method:
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgDust2"
android:clickable="false"
android:src="#drawable/dust2"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp"
android:onClick="imgDust2"/>

Android app hangs

Hey i'm brand new to programming and i need some help,
i'm doing a project for school and it's an android app where the user inputs words to a text field and then those words are used to create a story, like mad libs. I can not seem to see what is wrong with my code here when i debug on my phone it just shows a white screen. Could someone point out to me what i'm doing wrong? thanks...
MainActivity.java:
package com.joe.madlibs;
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.TextView;
public class MainActivity extends Activity {
EditText Noun, Adj, Name, Verb, Verb2, Noun2, Adj2;
Button Submit;
TextView Story;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Noun = (EditText) findViewById(R.id.noun1);
Adj = (EditText) findViewById(R.id.adjective1);
Name = (EditText) findViewById(R.id.name1);
Verb = (EditText) findViewById(R.id.verb1);
Verb2 = (EditText) findViewById(R.id.verb2);
Noun2 = (EditText) findViewById(R.id.noun2);
Adj2 = (EditText) findViewById(R.id.adj2);
String noun = Noun.getText().toString();
String adj = Adj.getText().toString();
String name = Name.getText().toString();
String verb = Verb.getText().toString();
String verb2 = Verb2.getText().toString();
String noun2 = Noun2.getText().toString();
String adj2 = Adj2.getText().toString();
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<EditText
android:id="#+id/noun1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1"
android:ems="10"
android:hint="#string/noun" >
</EditText>
<EditText
android:id="#+id/verb1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/name1"
android:layout_alignBottom="#+id/name1"
android:layout_alignRight="#+id/storyBox"
android:ems="10"
android:hint="#string/verb" />
<EditText
android:id="#+id/adjective1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/noun1"
android:layout_alignBottom="#+id/noun1"
android:layout_alignLeft="#+id/verb1"
android:ems="10"
android:hint="#string/adjective" />
<EditText
android:id="#+id/name1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/noun1"
android:layout_below="#+id/noun1"
android:ems="10"
android:hint="#string/name" />
<EditText
android:id="#+id/verb2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name1"
android:layout_below="#+id/name1"
android:ems="10"
android:hint="#string/verb" />
<EditText
android:id="#+id/adj2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/verb1"
android:layout_below="#+id/verb1"
android:ems="10"
android:hint="Adjective" />
<EditText
android:id="#+id/noun2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/verb2"
android:layout_below="#+id/verb2"
android:ems="10"
android:hint="Noun" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Click here to get your story!" />
<TextView
android:id="#+id/storyBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/noun2"
android:layout_below="#+id/button1"
android:layout_marginTop="18dp"
android:text="This is where your story will go!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Joe.madlibs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.Joe.madlibs.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Umm... where are you instantiating your Submit button before trying to set an onClickListener on it? I see where you declare it, but you aren't instantiating it.
Add:
Submit = (Button)findViewById(R.id.button1);
right before
Submit.setOnClickListener(new View.OnClickListener() {
and watch. It's gonna be like magic.
One more thing about coding conventions (style): when you name your instance variables, use the headlessCamelCase convention. Names with CamelCase/InitCaps (first letter of each word capitalized) should only be used when you name your class (the pattern for an object) as opposed to when you refer to an instance of that class (the actual object).
Wrong:
// An instance of Button class
Button Submit;
Right:
// An instance of Button class:
Button submit;
When it comes to compound names (more than one word), headlessCamelCase means you capitalize the first letter of each word, except the first word.
Right:
// An instance of Button class:
Button myVeryElderlyMotherJustSatUponNapoleonsSubmitButton;

Why isn't my TextView displaying its string properly?

I'm using an example out of the android programming book I'm going through. The point of this exercise is that when I click the "about" button, a new activity should start and display some text. For some reason that text is not showing up even though the text shows up in the graphical layout in my IDE. I'm using my phone as the emulator and my phone is running Android 4.0.3. I'm using eclipse. Here's my code:
Main activity:
package org.example.sodoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton= findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton= findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton= findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton= findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
Main xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:gravity="center"
android:padding="35dip">
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:textSize="24.5sp" />
<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button
android:id="#+id/continue_button"
android:text="#string/continue_label" />
<Button
android:id="#+id/new_button"
android:text="#string/new_game_label" />
</TableRow>
<TableRow>
<Button
android:id="#+id/about_button"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:text="#string/exit_label" />
</TableRow>
</TableLayout>
</LinearLayout>
About Class:
package org.example.sodoku;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
protected void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
About xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" >
</TextView>
</ScrollView>
[EDIT] Forgot about strings xml and manifest:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sudoku!</string>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sodoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<color name="background">#3500ffff</color>
<string name="exit_label">Exit</string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">fuck your ethnicity</string>
</resources>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sodoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Sudoku"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".About"
android:label="#string/about_title">
</activity>
</application>
</manifest>
Any help would be much appreciated, thank you.
You have misspelled onCreate() method in your About activity (compare OnCreate()and onCreate()), so you don't actually override base class method. Replace your onCreate with this one:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
It may help you. use this one as about.xml.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" >
</TextView>
</LinearLayout>
</ScrollView>

Categories

Resources