Why isn't my TextView displaying its string properly? - java

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>

Related

I am working on an Application which closes after the splash screen and the next activity was not loaded

i am working on a filter app and have created a splash screen and one other activity named 'ControlActivity' but as soon as the splash screen disappears the app closes without loading the 'ControlActivity'.
Also it does not gives any error closing the app.
The code for Main Activity is:-
package com.example.harsh.filters;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
},1000);
}
}
The XML code for the ControlActivity:-
<?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"
tools:background="#android:color/white"
tools:context=".ControlActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:padding="12dp"
android:theme="?attr/actionBarTheme" />
<ImageView
android:id="#+id/centerImageView"
android:layout_width="match_parent"
android:layout_height="315dp"
android:layout_alignParentTop="true"
android:layout_marginTop="55dp"
app:srcCompat="#mipmap/img1"
tools:layout_below="#+id/toolbar"
tools:padding="16dp" />
<LinearLayout
android:id="#+id/controlLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/centerImageView"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp"
tools:layout_below="#+id/centerImageView"
tools:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/uncheck" />
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/check" />
</LinearLayout>
<LinearLayout
android:id="#+id/thumbnailLinearLayout"
android:layout_width="match_parent"
android:layout_height="147dp"
android:orientation="horizontal"
android:padding="8dp"
tools:layout_below="#+id/controlLinearLayout">
<ImageView
android:id="#+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect1" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect2" />
<ImageView
android:id="#+id/imageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect3" />
<ImageView
android:id="#+id/imageView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect4" />
</LinearLayout>
</RelativeLayout>
**The ControlActivity:-**
package com.example.harsh.filters;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ControlActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
}
}
Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.harsh.filters">
<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"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_control"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Please help me out and thanks.

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"/>

Android retrieving a TextView results in Null. Have setContentView; Used correct id; have activity in Maifest

I'm getting a nullPointerE at run time, and have researched what problems could cause it, and I believe I suffer from none of them.
I believe the problem is that I have my layout in a fragment, and, though the app loads it correctly when I activate the intent, I think when I setContentView it doesn't find the view there. I've tried setting the fragment as the content view, but it crashes then as well. Also, I'm using a fragment for another screen and can access the view just fine. I thought it might be accessing the string from the intent, but I confirmed that I'm getting a null object when I use findViewById with the if statement in the onCreate method. The app runs just fine (without executing the inner code of course).
Here is my class:
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent prev = getIntent();
String tipOfTheDay = prev.getStringExtra(Login.EXTRA_TIP);
String welcome = prev.getStringExtra(Login.EXTRA_WELCOME);
TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);
if (welcomeScreen != null) {
welcomeScreen.setText(welcome + "\n" + tipOfTheDay);
}
}
Here is my activity_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wingman.Home"
tools:ignore="MergeRootFrame" />
Here is my fragment_home.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="com.example.wingman.Home$PlaceholderFragment" >
<TextView
android:id="#+id/textViewHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/empty" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton1"
android:layout_below="#+id/imageButton1"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/q_button" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton3"
android:layout_below="#+id/textView1"
android:layout_marginTop="40dp"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/calendar_button" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton2"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/tips_button" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton3"
android:layout_below="#+id/imageButton3"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/texts_button" />
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wingman"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.wingman.Login"
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="com.example.wingman.Home"
android:label="#string/title_activity_home" >
</activity>
</application>
Thanks for looking. I'm trying to do more research on fragments, but I honestly don't know if that's even the cause.
Your TextView is in the fragment xml, but when you are calling
TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);
it's looking for in the activity_home.xml
Try this to set your TextView
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_home, null)
TextView textView = (TextView) view.findViewById(R.id.textViewHome);
EDIT You can check this link for a tutorial on fragments

RuntimeError for activity specified on Android Manifest

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);
}

Categories

Resources