Can't instantiate class -android programming - java

I was following the tutorial videos here: http://www.youtube.com/watch?v=pZaRNVwKAy4&list=PLB03EA9545DD188C3&index=7 and came across a few errors while following the tutorials. Everything works fine until i started to work on the Tutorial: onCheckedChanged method. Can someone explain what I am doing wrong? Thanks a lot.
LogCat Code
09-15 18:38:22.444: E/MediaPlayer(1764): Should have subtitle controller already set
09-15 18:38:27.984: E/MediaPlayer(1764): Should have subtitle controller already set
09-15 18:38:31.374: E/AndroidRuntime(1764): FATAL EXCEPTION: main
09-15 18:38:31.374: E/AndroidRuntime(1764): Process: com.example.myfirstapp, PID: 1764
09-15 18:38:31.374: E/AndroidRuntime(1764): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.TutorialOne}: java.lang.InstantiationException: can't instantiate class com.example.myfirstapp.TutorialOne
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.os.Handler.dispatchMessage(Handler.java:102)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.os.Looper.loop(Looper.java:136)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-15 18:38:31.374: E/AndroidRuntime(1764): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764): at java.lang.reflect.Method.invoke(Method.java:515)
09-15 18:38:31.374: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-15 18:38:31.374: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-15 18:38:31.374: E/AndroidRuntime(1764): at dalvik.system.NativeStart.main(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764): Caused by: java.lang.InstantiationException: can't instantiate class com.example.myfirstapp.TutorialOne
09-15 18:38:31.374: E/AndroidRuntime(1764): at java.lang.Class.newInstanceImpl(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764): at java.lang.Class.newInstance(Class.java:1208)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-15 18:38:31.374: E/AndroidRuntime(1764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-15 18:38:31.374: E/AndroidRuntime(1764): ... 11 more
Main Java Code
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Main extends Activity {
// declare variables for the who class
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// start music command
logoMusic=MediaPlayer.create(Main.this, R.raw.melody);
logoMusic.start();
// start thread
Thread logoTimer=new Thread(){
public void run(){
try {
sleep(5000); // 1k is 1sec
Intent menuIntent=new Intent("com.example.myfirstapp.MENU");// address in manifest
startActivity(menuIntent);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally
{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
super.onPause();
logoMusic.release();
}
}
Menu Java code
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MediaPlayer buttonSound=MediaPlayer.create(Menu.this, R.raw.button);// make sure it is a final variable bc u are using it in sub
// set up buttons reference
Button tut1=(Button) findViewById(R.id.tutorial1);
Button tut2=(Button) findViewById(R.id.tutorial2);
// setting up the button functions
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.myfirstapp.TutorialOne"));// short cut in creating the intent
}
});
tut2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.myfirstapp.TutorialOne"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
TutorialOne java code
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
#SuppressLint("RtlHardcoded")
public abstract class TutorialOne extends Activity implements OnCheckedChangeListener{
// set up variables
TextView textOut;
EditText textIn;
RadioGroup gravityG,styleG;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
//similar to how you set the button to reference to xml
textOut=(TextView)findViewById(R.id.tvChange);
textIn=(EditText)findViewById(R.id.editText1);
gravityG=(RadioGroup)findViewById(R.id.rgGravity);
styleG=(RadioGroup)findViewById(R.id.rgStyle);
Button gen=(Button)findViewById(R.id.generate);
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// when you click generate, it will text out
textOut.setText(textIn.getText());// get user input and output
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
public void onCheckedChanged(CompoundButton buttonView, int isChecked) {
// TODO Auto-generated method stub
switch(isChecked){
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);// set gravity to left
break;
case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;
case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
break;
}
}}
main .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.Main" >
<Button
android:id="#+id/tutorial1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/tutorial2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:text="#string/button2" />
</RelativeLayout>
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/flash">
</LinearLayout>
tutorial1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text=""
android:textStyle="bold" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/style"
android:gravity="center" />
<TextView
android:id="#+id/tvGravity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/gravity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:id="#+id/rgStyle" >
<RadioButton
android:id="#+id/rbNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/normal" />
<RadioButton
android:id="#+id/rbItalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/italic" />
<RadioButton
android:id="#+id/rbBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bold" />
</RadioGroup>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:id="#+id/rgGravity" >
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/center" />
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/right" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/tvChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text_view_change"
android:textStyle="bold" />
<Button
android:id="#+id/generate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/generate"
android:textSize="25sp" />
</LinearLayout>
AndroidManifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
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=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.myfirstapp.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name= "com.example.myfirstapp.TutorialOne" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

Your TutorialOne Activity is abstract. You can't instantiate an abstract class. As an aside, you shouldn't be hardcoding the class name anyway. If that's what your tutorial is suggesting, then I suggest running far, far away.
Intent menuIntent = new Intent(Main.this, Menu.class);
startActivity(menuIntent);
EDIT: As for the onCheckedChanged(), you need to implement RadioGroup.OnCheckedChangeListener, not CompoundButton.OnCheckedChangeListener. The CompoundButton one uses a boolean while the RadioGroup one provides an integer.
import android.widget.CompoundButton.OnCheckedChangeListener;
should be
import android.widget.RadioGroup.OnCheckedChangeListener;

In order to use the the onCheckedChanged() method, you need a listener in order to get the method. Do so using an anonymous class listener defined by a method, or in the onCreate() method like so:
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
/*Do things here*/
}
});

thanks to everyone's help, I was able to generate a new code for the TutorialOne, but I do not know why I cannot get the options to work: LEFT,Right, Normal,Italic,Bold. Here is the new code for tutorial One, I also changed the Intent in the Main java according to what Kcoppock suggested.
Here is my new code :
package com.example.myfirstapp;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class TutorialOne extends Activity implements OnCheckedChangeListener
{
// set up variables
TextView textOut;
EditText textIn;
RadioGroup gravityG, styleG;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
// similar to how you set the button to reference to xml
textOut = (TextView) findViewById(R.id.tvChange);
textIn = (EditText) findViewById(R.id.editText1);
gravityG = (RadioGroup) findViewById(R.id.rgGravity);
styleG = (RadioGroup) findViewById(R.id.rgStyle);
Button gen = (Button) findViewById(R.id.generate);
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// when you click generate, it will text out
textOut.setText(textIn.getText());// get user input and output
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);// set gravity to left
break;
case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;
case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
break;
case R.id.rbNormal:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL),Typeface.NORMAL);
break;
case R.id.rbItalic:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
break;
case R.id.rbBold:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),Typeface.BOLD);
break;
}
}
}

Related

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tugas, PID: 12670

I just want to send data from activity1 to activity 2 using text input, where activity 1 makes data input and activity 2 displays data from activity 1,I have followed the full youtube tutorial but still the error and the error is no warning in the line of code, I don't understand if this is an error from android studio or a code writing error, here it is written :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tugas, PID: 12670
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tugas/com.example.tugas.dua}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
this my activity1 page MainActivity.java code:
package com.example.tugas;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText et;
String st;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.kirim);
et = findViewById(R.id.test);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(MainActivity.this,dua.class);
st = et.getText().toString();
intent.putExtra("Value",st);
startActivity(intent);
finish();
}
});
}
}
here my activity_main.xml code:
<?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"
android:padding="2sp">
<EditText
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Something" />
<Button
android:id="#+id/kirim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/test"
android:text="submit"
/>
</RelativeLayout>
this is my second activity dua.java code:
package com.example.tugas;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class dua extends AppCompatActivity {
TextView textView;
String st;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dua);
textView = findViewById(R.id.test);
st = getIntent().getExtras().getString("Value");
textView.setText(st);
}
}
here my second activity layout activity_dua.xml code:
<?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=".dua">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Value"
android:textColor="#color/black"
android:textSize="30sp"/>
</RelativeLayout>
this my AndroidManifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.tugas">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Tugas"
tools:targetApi="31">
<activity
android:name=".dua"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
thank you for your attention
Simply.You don't define the TextView object id In the dua (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"
tools:context=".dua">
<TextView
android:id="#+id/test" <--
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Value"
android:textColor="#color/black"
android:textSize="30sp"/>
</RelativeLayout>

how can i play sound in android

I wrote simplw android program to play sound and it's not working . Have no idea why .
maybe it's related to the fact that i have question mark near mymp3 file ( under raw directory ).
I tried few options without any suseess .
Attached below is my code , thanks in advance .
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:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="#CFEC89"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="200dp"
android:layout_height="80dp"
android:id="#+id/sound"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_gravity="center"
android:text="Sound"
android:textSize="30sp"
android:background="#EAE3E3"
/>
</LinearLayout>
main acitivy java
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
// define my variables
Button sound;
final MediaPlayer mp = MediaPlayer.create(this,R.raw.cat);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v)
{
mp.start();
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playsound">
<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.PlaySound">
<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>
First of all, delete your TextView it could lay over your Button. That's why you probably can't click it.
Paste that in your class:
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.cat);
final Button sound = (Button) this.findViewById(R.id.sound);
sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
}
For the future, you need to define your variables in the OnCreate and not above.

Android Studio: Code is done, but app is crashing everytime I try to run

I've made an app that allows user to login but everytime I try to run, my App is crashing constantly.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mertino11.ourapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_account.xml (XML for user logged in succesfully)
<?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/activity_account"
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.mertino11.ourapplication.AccountActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Account Page"
android:ems="10"
android:id="#+id/editText"
android:textSize="22sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textStyle="normal|bold"
android:textAlignment="center" />
</RelativeLayout>
activity_main.xml (login page)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:baselineAligned="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailField"
android:hint="Email"
android:paddingTop="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordField"
android:hint="Password"
android:fontFamily="sans-serif"
android:paddingTop="20dp" />
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/loginBtn"
android:paddingTop="20dp" />
</LinearLayout>
Java --> Class: AccountActivity (Page when the user logged in successfully)
package com.example.mertino11.ourapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class AccountActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
}
}
Java --> Class: FireApp (Firebase settings I think)
package com.example.mertino11.ourapplication;
import android.app.Application;
import com.firebase.client.Firebase;
/**
* Created by Mertino11 on 10-Dec-16.
*/
public class FireApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Java --> Class: Main Activity (Back-end logging with account)
package com.example.mertino11.ourapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private EditText mEmailField;
private EditText mPasswordField;
private Button mLoginBtn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passwordField);
mLoginBtn = (Button) findViewById(R.id.loginBtn);
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, AccountActivity.class));
}
}
};
mLoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSignIn();
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void startSignIn() {
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
Error Message at Run:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mertino11.ourapplication, PID: 3060
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.AccountActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.example.mertino11.ourapplication.MainActivity$1.onAuthStateChanged(MainActivity.java:48)
at com.google.firebase.auth.FirebaseAuth$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.
Screenshot of the error:
http://i66.tinypic.com/51btrp.png
The scenario of the app:
Create account firebase Google (manually)
Opens app --> Sees login page
Logs in with accountdetails of Firebase
Goes to AccountActivity page
Note: I am a amateur/beginner with AndroidStudio.
The error is pretty self-explanatory.
You have missed adding the AccounActivity on your manifest...
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".AccountActivity"
android:theme="#style/AppTheme" />
</application>
Add AccountActivity in your AndroidManifest.xml
<activity
android:name=".AccountActivity">
<intent-filter>
...
</intent-filter>
</activity>
Always carefully read logcat:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.AccountActivity}; have you declared this activity in your AndroidManifest.xml?
I thank you all for giving me the solution. However (If it is possible) do you guys know why my App is running AccountActivity first instead of my Mainactivity? (it should run the mainactivity first)?
add .AccountActivity in manifest file as
<activity
android:name=".AccountActivity">
</activity>

Android Hello World Tutorial stops when tested

I am up to the "Start Another Activity" section of the Android tutorial and it simply won't work when I install and test it.
It compiles fine but running it breaks after I click the send button.
I am using the command line tools on Ubuntu 12.04 and installing to a real device, my Galaxy S5.
I am aware of logcat but haven't been able to get it working, it either shows no output at all, or gives a massive spam of output I can't keep up with. I would happily provide logcat information if I could manage to isolate my app's output and cut through everything else.
I have seen a lot of similar questions on this which leads me to believe the tutorial isn't very well written.
Here is "MyActivity.java"
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.view.View;
public class MyActivity extends Activity
{
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
/** Called when the activity is first created */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called when the user clicks the send button */
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Here is DisplayMessageActivity
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import com.example.myfirstapp.R;
public class DisplayMessageActivity extends ActionBarActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Get the message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//Set the text view as the activity layout
setContentView(textView);
}
}
Here is AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity
android:name="MyActivity"
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.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MyActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MyActivity"
/>
</activity>
</application>
</manifest>
EDIT: Here is Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
There was some code that I changed and took out as it said to add code in, but then would later show a "this is what your code should look like" and it was missing some of the original code. I also changed the manifest package names in the activities because at the beginning it uses com.example.myfirstapp but later uses com.mycompany.myfirstapp
Any help or advice on why this seemingly simple tutorial doesn't work is greatly appreciated.
EDIT:
Logcat output - (after pressing the send button)
I/Timeline( 3812): Timeline: Activity_launch_request id:com.example.myfirstapp time:74636924
D/AndroidRuntime( 3812): Shutting down VM
E/AndroidRuntime( 3812): FATAL EXCEPTION: main
E/AndroidRuntime( 3812): Process: com.example.myfirstapp, PID: 3812
E/AndroidRuntime( 3812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
E/AndroidRuntime( 3812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
E/AndroidRuntime( 3812): at android.app.ActivityThread.access$900(ActivityThread.java:172)
E/AndroidRuntime( 3812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
E/AndroidRuntime( 3812): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3812): at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime( 3812): at android.app.ActivityThread.main(ActivityThread.java:5834)
E/AndroidRuntime( 3812): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3812): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 3812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
E/AndroidRuntime( 3812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
E/AndroidRuntime( 3812): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
E/AndroidRuntime( 3812): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:16)
E/AndroidRuntime( 3812): at android.app.Activity.performCreate(Activity.java:6221)
E/AndroidRuntime( 3812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
E/AndroidRuntime( 3812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
E/AndroidRuntime( 3812): ... 10 more
I/Process ( 3812): Sending signal. PID: 3812 SIG: 9
I/ActivityManager( 871): Process com.example.myfirstapp (pid 3812)(adj 13) has died(104,205)
Here's the working code :
MainActivity.class
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
}
public void Go(View v)
{
Log.d("check", "Pressed");
String value = et.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", value);
startActivity(intent);
}
}
SecondActivity.class
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.TextView;
public class SecondActivity extends ActionBarActivity {
TextView textv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);
String txt = getIntent().getStringExtra("key");
Log.d("check", "got : " +txt);
textv = (TextView) findViewById(R.id.textView1);
textv.setText(txt);
}
}
res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.testproject.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="67dp"
android:layout_marginTop="46dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="Go"
android:text="Button" />
</RelativeLayout>
res/layout/new_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".SecondActivity"></activity> // Registered the newly created activity
</application>
Output :
Logcat :
BY SEEING YOUR LOGCAT, I got there is a problem in your Theme.
You didn't declared AppTheme in AndroidManifest.xml file
android:theme="#style/AppTheme"
Try by This Sample Code I have made For You.
mainactivity.xml Code
<EditText
android:id="#+id/edtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="message"
/>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>
MainActivity.class
Button btnSend;
EditText edtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtMessage=(EditText)findViewById(R.id.edtMessage);
btnSend=(Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String Message=edtMessage.getText().toString();
Intent intent=new Intent(getApplicationContext(),Second.class);
intent.putExtra("Message", Message);
startActivity(intent);
}
});
}
second.xml
<TextView
android:id="#+id/tviMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
Second.class
TextView tviMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tviMessage=(TextView)findViewById(R.id.tviMessage);
Bundle extras = getIntent().getExtras();
String message = extras.getString("Message");
tviMessage.setText(message);
}
Manifesto.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".Second"
android:label="#string/app_name" >
</activity>
By using logcat with this command:
adb logcat | grep `adb shell ps | grep com.example.myfirstapp | cut -c10-15`
I was able to find the error causing the app to crash. It came from missing the line:
android:theme="#style/Theme.AppCompat.Light"
In the Manifest.xml file. This is required by the Activity bar compatibility class. This part was completely overlooked in the tutorial, presumably because IDEs do this part automatically.
Thank you to everybody for their help and suggestions, it helped me keep looking and trying things until I finally found my solution.

App showing error "myapp has stopped"

This is my first time in creating an android app and this happens.
Any help is appreciated.
Here is my updated logcat after changing the manifest
04-10 16:00:17.154: E/AndroidRuntime(2480): FATAL EXCEPTION: main
04-10 16:00:17.154: E/AndroidRuntime(2480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miraapp/com.example.miraapp.MainActivity}: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.os.Looper.loop(Looper.java:137)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 16:00:17.154: E/AndroidRuntime(2480): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 16:00:17.154: E/AndroidRuntime(2480): at dalvik.system.NativeStart.main(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480): Caused by: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.example.miraapp.MainActivity.onCreate(MainActivity.java:30)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.Activity.performCreate(Activity.java:5104)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 16:00:17.154: E/AndroidRuntime(2480): ... 11 more
Here is my code and i have another one for the new activity
package com.example.miraapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
Button button1;
EditText etResponse;
TextView tvIsConnected;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
}
private void button1Click()
{
startActivity(new Intent("com.example.miraapp.GUI"));
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
button1Click();
break;
case R.id.button2:
button2Click();
break;
}
}
private void button2Click()
{
}
};
btw, this is my activity_main
<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"
android:background="#000000"
tools:context="com.example.miraapp.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="131dp"
android:text="#string/START_fix"
android:onClick="button1Click"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:contentDescription="#string/title_fix"
android:src="#drawable/mira4" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="#string/CONNECT_fix" />
</RelativeLayout>
here is my updated androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miraapp"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17"
/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.miraapp.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="com.example.miraapp.GUI"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.miraapp.GUI" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Try this..
Remove android:name="Mira" from mainfest inside application like below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
And one more thing..
Change startActivity(new Intent("com.example.miraapp.GUI")); to startActivity(new Intent(MainActivity.this,GUI.class)); like below in button1Click() method
private void button1Click()
{
startActivity(new Intent(MainActivity.this,GUI.class));
}
EDIT
public class MainActivity extends Activity implements OnClickListener
And then
change button1.setOnClickListener((OnClickListener) this); as
button1.setOnClickListener(this);
Application
Base class for those who need to maintain global application state.
You can provide your own implementation by specifying its name in your
AndroidManifest.xml's application tag, which will cause that class
to be instantiated for you when the process for your
application/package is created.
So you have
<application
android:name="Mira
and the stacktrace says
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.miraapp.Mira" on path:
/data/app/com.example.miraapp-2.apk
You also say
i only have the class MainActivity
So get rid of android:name="Mira from application tag
Also you need to use Explicit intents
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
SO use
startActivity(new Intent(this,GUI.class);
Since you have explicit intents you can get rid of intent-filter
<intent-filter>
<action android:name="com.example.miraapp.GUI" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Edit:
Caused by: java.lang.ClassCastException:
com.example.miraapp.MainActivity cannot be cast to
android.view.View$OnClickListener
Change this
public class MainActivity extends Activity
to
public class MainActivity extends Activity implements OnClickListener
And then change this
button1.setOnClickListener((OnClickListener) this);
// this refers to the Activity. but you cast it to (OnClickListener) this
to
button1.setOnClickListener(this);
You need your class to implement interface OnClickListener
So you need to have button1.setOnClickListener(this);
MainActivity.java
package com.example.miraapp;
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;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button button1;
EditText etResponse;
TextView tvIsConnected;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
private void button1Click() {
startActivity(new Intent(this, GUI.class));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
button1Click();
break;
case R.id.button2:
button2Click();
break;
}
}
private void button2Click() {
}
};
manifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miraapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.miraapp.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="com.example.miraapp.GUI"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
GUI.java
package com.example.miraapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class GUI extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(), "hello", 1000).show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="94dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="44dp"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="46dp"
android:layout_marginRight="34dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>

Categories

Resources