Number picker only allowing zero as value - java

I am making an app that utilizes the numberpicker. What happens is when I try to create the numberpicker and set its attributes, it just simply won't do anything. The only value it allows is zero, and you can't scroll the numbers with it either. After a lot of google searches, I'm quite certain that I put the min/max-value methods in the right order.
I would appreciate some guidance of what I'm doing wrong.
source:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
public class Start extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
NumberPicker minutePicker = (NumberPicker) findViewById(R.id.minutePicker);
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
NumberPicker secondPicker = (NumberPicker) findViewById(R.id.secondPicker);
secondPicker.setMaxValue(60);
secondPicker.setMinValue(0);
secondPicker.setWrapSelectorWheel(false);
}
}
edit:
Added XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/twoPickers"
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.listifymusic.listifymusic.Start">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginStart="45dp"
android:id="#+id/secondPicker"
android:layout_alignBaseline="#+id/minutePicker"
android:layout_alignBottom="#+id/minutePicker"
android:layout_toRightOf="#+id/minutePicker"
android:layout_toEndOf="#+id/minutePicker" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minutePicker"
android:layout_marginLeft="102dp"
android:layout_marginStart="102dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Make sure your activity is set as launcher activity in your manifest.xml:
<activity android:name=".Start">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

You've started some other Activity that is essentially doing only this.
public class SomeOtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
In other words, the code you have should work if you start the Start activity.

Related

Android Studio Application Crashes When Opening Second Activity [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
findViewById() returns null when I call it in onCreate()
(3 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I am trying to open up the second activity (MainActivity2) via a button but the application crashes every time, I am not quite sure how to solve this. I want to be able to open to the second screen to use the second activity. Thank you for your help. (Having trouble with separating the code, so I have used lines)
Activity main xml + Main activity
<?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:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="241dp"
android:text="Activity1"
android:id="#+id/textView"/>
<Button
android:id="#+id/button"
android:layout_width="375dp"
android:layout_height="101dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="275dp"
android:text="open password generator" />
</RelativeLayout>
**Main activity**
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
activity main2 + activity main 2 XML + manifest
<?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:context=".MainActivity2">
<TextView
android:id="#+id/textView3"
android:layout_width="203dp"
android:layout_height="85dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="67dp"
android:layout_marginLeft="67dp"
android:layout_marginTop="264dp"
android:hapticFeedbackEnabled="false"
android:textColor="#070505"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.653"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginBottom="304dp"
android:hapticFeedbackEnabled="true"
android:soundEffectsEnabled="false"
android:text="Generate strong password"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="#36A13A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.59" />
</RelativeLayout>
***Mainactivity2***
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity2 extends AppCompatActivity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(generateString(16));
}
});
}
private String generateString(int length) {
char[] chars ="/.,MNBVCXZ;LKJHGFDSAPOIUYTREWQ[]#=-)0987654321!£$*&qwertyuiopzxcvbnmasdfghjkl(".toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
for ( int i=0; i<length;i++)
{
char c = chars[random.nextInt(chars.length)];
stringBuilder.append(c);
}
return stringBuilder.toString();
}
}
***AndroidManifest xml***
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.menutest">
<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/Theme.MENUTEST">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
..........................................................................
Mainactivity2
button = (Button)findViewById(R.id.button4);
textView = (TextView)findViewById(R.id.textView3);

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

Cannot switch activities in Android except one activity

So I have been making a simple four button app for my IT 1 class and have been having a problem with some code. I have four buttons that are supposed to call a startActivity and change to a different screen. The first button, labeled fire, works perfectly fine and works 100% of the time. The next button, labeled Earthquake doesn't work, even though the code is the same (without the parts that need to change). The other 2 buttons also do not work. I have tried making a new project, copying the working xml, copying the exact code. I cannot figure out why it doesn't work.
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<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:background="#81848b"
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.carrieowen.testproject.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#eff2f9"
android:text="Emergency Drills"
android:gravity="center"
android:textSize="32sp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="40dp"
android:text="Fire"
android:background="#e71e1e"
android:id="#+id/button"
android:onClick="Fire"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#009c12"
android:padding="40dp"
android:text="Earthquake"
android:id="#+id/button2"
android:onClick="Earthquake"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#eef035"
android:padding="40dp"
android:text="Tornado"
android:id="#+id/button3"
android:onClick="Tornado"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#1b4fdd"
android:padding="40dp"
android:text="Code Blue"
android:id="#+id/button4"
android:onClick="CodeBlue"/>
</LinearLayout>
</RelativeLayout>
The MainActivity Java
package com.example.carrieowen.testproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Fire (View view){
Intent changeScreenFire = new Intent(this, firescreen.class);
startActivity(changeScreenFire);
}
public void Earthquake (View view){
Intent changeScreenEarthquake = new Intent(this, earthquake.class);
startActivity(changeScreenEarthquake);
}
public void Tornado (View view){
Intent changeScreenTornado = new Intent(this, tornado.class);
startActivity(changeScreenTornado);
}
public void CodeBlue (View view){
Intent changeScreenCodeBlue = new Intent(this, codeblue.class);
startActivity(changeScreenCodeBlue);
}
}
The FireScreen Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class firescreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firescreen);
}
}
The Earthquake Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class earthquake extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earthquake);
}
}
The XML for the Fire and Earthquake are the same other than the Text boxes being different. If needed will add.
Make sure your activities are listed in the manifest? The manifest file is called AndroidManifest.xml and is listed in the project at the top, in android studio. This is what the content should look like:
<?xml version="1.0" encoding="utf-8"?>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Tornado">
</activity>
<activity android:name=".CodeBlue">
</activity>
</application>
I have something like that as well, but my code is a bit different. Try it like this. The button that you used to create open the new activity is labeled in the line starting with Button Pause, then i set a onclicklistner so that when you click on it, something happens, and i have it to currently oped a new activity in the method past the #Override part.
public class GameWindow extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_window);
Button pause = (Button) findViewById(R.id.pausebutton);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
Intent intent = new Intent(GameWindow.this, PauseMenu.class);
startActivity(intent);
}
});
}

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

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