i have read a lot about this but didnt come up with a solution.
this is my first android project.
i am trying yo open an activity from my main activity.
i have doubled check the names and the configuration files, but the startactivity() procedure fails.
debbuger shows runtime errror.
as u will c, the startactivity is called upon onclick event.
i am really stuck here dont know what else to do.
thanks a lot!
* this is the main activity **
package com.example.helloandroid;
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;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private OnClickListener gtScoringListener = new OnClickListener() {
public void onClick(View v) {
Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
startActivity(mintent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(gtScoringListener);
}
}
* this is the target activity: *
package com.example.helloandroid;
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.TextView;
public class imgScoring extends Activity {
private OnClickListener backBtnListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
startActivity(intent);
}
};
private OnClickListener scoreBtnListener = new OnClickListener() {
public void onClick(View v){
TextView tv = (TextView)findViewById(R.id.editText1);
tv.setText(((Button)v).getText());
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button03);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button04);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.button3);
button.setOnClickListener(backBtnListener);
setContentView(R.layout.imgscoring);
}
}
this is the androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".imgScoring"></activity>
<activity android:name=".HelloworldActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should've posted the stacktrace, but I assume the error is on this line in your target activity:
button.setOnClickListener(scoreBtnListener);
This will give you a NULL:
Button button = (Button)findViewById(R.id.button1);
because you haven't set a contentview in THAT activity, so there is nothing for findViewById() to return. You'll get a nullpointer when calling that setOnclickLIstener()
Try change class name from imgScoring to ImgScoring everywhere. Java is not allowing class name to start from small letter ;-)
And place setContentView(R.layout.imgscoring); just after your super.onCreate();
Related
I am new in android and when i run application i got this exception:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {com.example.activity/com.example.activity.Second};
have you declared this activity in your AndroidManifest.xml?
so when i want to view an activity from anther activity i got this error.
(I used Lynda to learn android and my sdk version is 24.0.2 , adt is 23.0.6 , and use eclips mars)
main activity java code:
package com.example.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
second activity java code :
package com.example.activity;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
manifest XML Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<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=".second"></activity>
</application>
</manifest>
by the way , when i change my main activity java code like below.everything works fine :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.second);
}
});
}
}
Android is case-sensitive. Replace:
<activity android:name=".second"></activity>
with:
<activity android:name=".Second"></activity>
In your manifest
<activity android:name=".Second"></activity>
Try this.
I attempted to run "Clean Code" according to this link, however nothing is happening. From being a Renegade developer my intuition says "Button onClick" is missing but I could be wrong. Here is my code written according to what the instructions are:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class BuildingaSimpleUserInterfaceApi8Activity extends Activity {
public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**This is how I work with Buttons and the Next Activity, however this method IS NOT defind
* in the very first Android Tutorial for sending information onClick.
Button bButtonone = (Button) findViewById(R.id.button_send);
bButtonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"));
}
});
}
*/
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
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);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.android.com_training_basics_firstapp_building_ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BuildingaSimpleUserInterfaceApi8Activity"
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.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" />
</application>
</manifest>
It looks like you forgot the line to start the activity:
public void sendMessage(View view) {
...
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); // add me!
}
I read through the code at the site listed and you have missed one line:
startActivity(intent);
at the end od sendMessage.
I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
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;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
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"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 2 Activities, one which is MainActivity and the other, ReadActivity.
Whenever I started ReadActivity from MainActivity, the Activity started but it shows nothing, just the android:label value in the Manifest file.
As I was investigating, I tried putting a Log in ReadActivity but I noticed that the OnCreate method isn't called as I forgot to override the method but when I put the "#Override" above the OnCreate method I get the followong error:
"The method OnCreate(Bundle) of type ReadActivity must override or implement a supertype method".
I don't get what's wrong with this line, I've crossed checked it so many times, compared the codes with other application's code but I just can't seem to override it. Can anyone help me? Below are the codes of my project.
MainActivity
package com.rangga.elearning.ngaji;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
/** Called when the activity is first created. */
TextView txtLogo, txtVersion;
Button btnRead, btnIndex, btnAbout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
//Typeface font = Typeface.createFromAsset(getAssets(), "assets/Schoolbell.ttf");
txtLogo = (TextView) findViewById(R.id.txtLogo);
txtVersion = (TextView) findViewById(R.id.txtVersion);
btnRead = (Button) findViewById(R.id.btnRead);
btnIndex = (Button) findViewById(R.id.btnIndex);
btnAbout = (Button) findViewById(R.id.btnAbout);
/* txtLogo.setTypeface(font);
txtVersion.setTypeface(font);
btnRead.setTypeface(font);
btnIndex.setTypeface(font);
btnAbout.setTypeface(font); */
btnRead.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
callIntent();
}
});
}
public void callIntent(){
Intent i = new Intent(this, ReadActivity.class);
startActivity(i);
}
}
ReadActivity
package com.rangga.elearning.ngaji;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class ReadActivity extends Activity{
private static String TAG = "ReadActivity";
#Override
public void OnCreate(Bundle savedInstanceState){
Log.i(TAG, "ReadActivity dimulai");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.read);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rangga.elearning.ngaji"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="7"
android:maxSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ReadActivity"
android:label="Baca"
android:screenOrientation="landscape">
</activity>
</application>
</manifest>
Thank you.
onCreate() not OnCreate() . Besides, your manifest doesn't contain Intent information for your ReadActivity.
I am reading a android book for beginners, and while following on of the chapters I ran into a problem. The chapter is teaching about intents. I right now I have 2 layouts: main.xml, and digital_clock.xml. And in the AndroidManifest I have these lines of code:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".Chapter11Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DigitalClockActivity" >
</activity>
</application>
Also I have the two Activity classes that correspond to the layouts:
Chapter11Activity:
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;
public class Chapter11Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button a2 = (Button)findViewById(R.id.button1);
a2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), DigitalClockActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
DigitalClockActivity:
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;
public class DigitalClockActivity extends Activity {
public void OnCreate(Bundle sIS) {
super.onCreate(sIS);
setContentView(R.layout.digital_clock);
Button a1 = (Button) findViewById(R.id.button01);
a1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
}
When I run the app on my phone and switch to the second activity it shows nothing at all. Am I defining something wrong in the AndroidManifest? The application seems very straight forward, but it is not working. I have check over to make sure I didn't type anything wrong. Is it because I am running a android 2.3.3 phone and using the 1.5 sdk, and something is not backwards compatible? Any answers are appreciated!
~Andrew
Method name in second activity should be onCreate not OnCreate.
To prevent typos like this in the future use #Override on methods that you are overriding:
#Override
public void OnCreate(Bundle sIS) {
// code here
}
Then if you make a typo, the compiler will tell about it.