So Ive made my first animation screen for my phone, this works perfect, and now I wanted to try it out on my tablet, but it crashes.
The tablet is a Lenovo TAB3 7 Essential / Samsung SM-T550.
As you can see in the picture above it's line 34 setContentView(R.layout.activity_startscreen); the error occurs.
I've tried to Clean my project, but i think it might be in the .xml file?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#73FFFFFF"
android:orientation="vertical"
tools:context=".activitys.Startscreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/first_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:background="#drawable/blue_line" />
<View
android:id="#+id/second_line"
android:layout_width="20dp"
android:layout_height="200dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/first_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/third_line"
android:layout_width="20dp"
android:layout_height="150dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/second_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/fourth_line"
android:layout_width="20dp"
android:layout_height="250dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/third_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/fifth_line"
android:layout_width="20dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fourth_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/six_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fifth_line"
android:background="#drawable/gray_line" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp">
<ImageView
android:id="#+id/wrd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:srcCompat="#drawable/logo_wrd" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-black"
android:text="Wireless Rehab Device"
android:textColor="#676767"
android:textSize="24sp" />
</RelativeLayout>
</LinearLayout>
The activity I showed Startscreen.java
public class Startscreen extends AppCompatActivity {
private static int SPLASH_DURTION_OUT = 5000; // 5000ms = 5s
//Views
View first, second, third, fourth, fifth, sixth;
TextView bottomTag;
ImageView wrd;
//Animations
Animation topAnimation, bottomAnimation, middelAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_startscreen);
topAnimation = AnimationUtils.loadAnimation(this, R.anim.top_animation);
bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);
middelAnimation = AnimationUtils.loadAnimation(this, R.anim.middel_animation);
first = findViewById(R.id.first_line);
second = findViewById(R.id.second_line);
third = findViewById(R.id.third_line);
fourth = findViewById(R.id.fourth_line);
fifth = findViewById(R.id.fifth_line);
sixth = findViewById(R.id.six_line);
wrd = findViewById(R.id.wrd);
bottomTag = findViewById(R.id.tag);
//Animation settings
first.setAnimation(topAnimation);
second.setAnimation(topAnimation);
third.setAnimation(topAnimation);
fourth.setAnimation(topAnimation);
fifth.setAnimation(topAnimation);
sixth.setAnimation(topAnimation);
wrd.setAnimation(middelAnimation);
bottomTag.setAnimation(bottomAnimation);
//Splash Screen
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(Startscreen.this, Home.class); //Home = Mainactivity
startActivity(intent);
finish();
}
}, SPLASH_DURTION_OUT);
}
}
This is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrd">
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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=".activitys.Home" />
<activity android:name=".activitys.Startscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ble.BLE_Service"
android:enabled="true" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Found the issue, all my .png images was in my Drawable-v24 folder and not Drawable, so it's fixed now!
Related
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.
I have first activity from which I start another one.
I created second one with:
Right click on app inside project explorer
New > Activity > Empty Activity
and with this I got class file inside Java folder and activity_newActivity.xml
I have created some stuffs inside that activity and struggling why functions are not working when I try to run them from my class but then I tried to add Log.d("Testing", "TEST") inside onCreate function and what I have found out is that it doesn't even run it. Why is this happening?
Whole class:
package rs.termodom.www.termodom;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;
import android.widget.TextView;
public class pocetna extends AppCompatActivity {
Display display;
Point screenSize;
TextView tp_txt;
ImageView test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pocetna);
Log.d("PROBLEM", "PROBLEM");
//==========================================================================================
//==========================================================================================
//==========================================================================================
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
tp_txt = (TextView) findViewById(R.id.tp_txt);
tp_txt.setText("StA");
test = (ImageView)findViewById(R.id.akcija1_img);
test.getLayoutParams().height = 150;
test.getLayoutParams().width = screenSize.x;
//==========================================================================================
//==========================================================================================
//==========================================================================================
}
}
Here is manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rs.termodom.www.termodom">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
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=".pocetna"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
and XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="rs.termodom.www.termodom.pocetna">
<android.support.constraint.ConstraintLayout
android:id="#+id/header"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#android:color/holo_blue_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/userName_txt"
android:layout_width="151dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="KORISNIK 123"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="1600 TP"
android:textColor="#android:color/holo_red_dark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/akcijaSlider"
android:layout_width="411dp"
android:layout_height="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/akcija1_img"
android:layout_width="268dp"
android:layout_height="112dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Did you register the activity in application manifest (AndroidManifest.xml) file?
I think that the problem is here
display.getSize(screenSize);
The screenSizeobject isn't initialize.
Try this:
screenSize = new Point();
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
Problem was I wasn't calling new activity properly.
I should have called it like this:
Intent myIntent = new Intent(getApplicationContext(), pocetna.class);
startActivity(myIntent);
I am trying to create 8 buttons in one activity that will open their own activity. What am I missing? I am using Android Studio 1.1.0.
I get this error
java.lang.IllegalStateException:
Could not find a method AppleActivity(View) in the activity class
com.hashmi.omar.vodafonemobilephoneshop.Picker for onClick handler
on view class android.widget.Button with id 'button2'
Below is the Picker.class:
package com.hashmi.omar.vodafonemobilephoneshop;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.hashmi.omar.vodafonemobilephoneshop.util.SamsungActivity;
public class Picker extends Activity implements View.OnClickListener {
Button button2, button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker);
//Sets the font to Vodafone Light
Typeface vodaLt = Typeface.createFromAsset(getAssets(), "VODAFONELT.TTF");
TextView vodaHeading = (TextView)findViewById(R.id.textView4);
vodaHeading.setTypeface(vodaLt);
//Sets up a button
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void buttonsam(){
startActivity(new Intent(Picker.this, SamsungActivity.class));
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.button2:
buttonapple();
break;
case R.id.button3:
buttonsam();
break;
}
}
}
Below is the activity_picker.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: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="com.hashmi.omar.vodafonemobilephoneshop.Picker"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please choose a brand"
android:id="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffff0000"
android:textSize="31dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
android:onClick="#string/title_activity_apple" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/samselect"
android:id="#+id/button3"
android:layout_column="20"
android:onClick="#string/title_activity_samsung" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/sonyselect"
android:id="#+id/button4"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/htcselect"
android:id="#+id/button5"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/bbselect"
android:id="#+id/button6"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nokiaselect"
android:id="#+id/button7"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nexselect"
android:id="#+id/button8"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/lgselect"
android:id="#+id/button9"
android:layout_column="20" />
</TableRow>
</TableLayout>
Here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Picker"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AppleActivity"
android:label="#string/title_activity_apple"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.AppleActivity" />
</activity>
<activity
android:name=".SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
<activity
android:name=".util.SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
</application>
Remove the simply onClick form your button2 because you are implemententing View.OnClickListener.
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
/>
You have written this:
<Button
... other stuff here ...
android:onClick="#string/title_activity_apple" />
If you use #string/title_activity_apple as the value of an attribute, Android will look up the string, and act as if that string's value was the actual value. So if the string has the value "AppleActivity", then it's equivalent to writing android:onClick="AppleActivity".
Next: what does setting android:onClick do? It tells the button the name of a method to call on the current activity when the button is clicked - see here. So you have told the button to call the method AppleActivity in the current activity when the button is pressed. (As you can also see on that page, it also expects to call a method with one parameter of type View - the arguments matter too)
Your activity class (the one containing the button0 does not have a method called AppleActivity which takes a View parameter. So the button can't find the method you told it to call, so it throws an exception (which crashes the app). This is exactly what the exception message said - the button could not find a method AppleActivity(View) in the activity class com.hashmi.omar.vodafonemobilephoneshop.Picker, which it was looking for because of your onClick attribute, on a android.widget.Button with id 'button2'.
You need to create Method named in your xml file in your activity class as well. instead of creating following method
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void title_activity_apple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
After above change check again.
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
I have been on this error now for a while. I am trying to open a list view in my dialog but its not opening for me this is my code.
LOGCAST
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mybasicapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Slashscreen"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".menu"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".tutorialsone"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.mybasicapp.TUTORIALSONE" />
<category android:name="android.intent.category.DEFAULT" />
Java code:
public class menu extends Activity{
Button ImagebuttonOpenDialog;
String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
String[] listContent = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagebuttonOpenDialog = (Button)findViewById(R.id.imgbtn1);
ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(menu.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnCancelListener",
Toast.LENGTH_LONG).show();
}});
dialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnDismissListener",
Toast.LENGTH_LONG).show();
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
parent.getItemAtPosition(position).toString() + " clicked",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});
break;
}
return dialog;
}
#Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
//
break;
}
}
}
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:background="#drawable/carbon" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:background="#drawable/bottombar" >
</FrameLayout>
<ImageButton
android:id="#+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="79dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgbtn1"
android:layout_marginRight="46dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn1"
android:layout_below="#+id/imgbtn1"
android:layout_marginTop="36dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn2"
android:layout_alignTop="#+id/imageButton3"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton3"
android:layout_below="#+id/imageButton3"
android:layout_marginTop="49dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton4"
android:layout_alignTop="#+id/imageButton5"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton5"
android:layout_below="#+id/imageButton5"
android:layout_marginTop="42dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton6"
android:layout_alignTop="#+id/imageButton7"
android:src="#android:drawable/btn_star" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kj" />
custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:minWidth="300dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ListView
android:id="#+id/dialoglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/kj" />
</LinearLayout>
ALSO
I looked over the java code again and found that my code had (button) where in my XML had image button i have change my java code so you can see where i am at the moment..still no luck.I don't know how to copy log cast
From my count, buttonOpenDialog is null so you are getting NPE when you try to add a listener to it. Check your activity_main that you have a Button with an id of im1. You probably misspelled the id or that one simply doesn't exist in your layout. If you don't see a problem with that then post the activity_main.xml.
Also, this isn't your problem but you should consider using Java standard naming conventions. Class names should start with a capital letter. So you would have Main instead of main as your class name.
When you post a question please post the logcat in your question as it always makes it easier for members to read then needing to follow a link that sometimes isn't of the best quality.