An error during instantiate intent - android

I am totally new to android programming and I was reading a book called "Hello Android"
Basically the book teaches us Android by using a Sudoku game example.
Here is the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label" />
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_game_label" />
<Button
android:id="#+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label" />
</LinearLayout>
</LinearLayout>
Here is the string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<color name="background">#3500ffff</color>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>
Here is the additional manifest to the old:
<activity android:name=".About"
android:label="#string/about_title" >
</activity>
Here is the about.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>
And finally the Sudoku.java
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener((OnClickListener) continueButton);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener((OnClickListener) this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener((OnClickListener) this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class); /** Here is the Error **/
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Let me explain it a little bit.
The Sudoku main page consists of 4 buttons, the book is teaching us to implement the button, so when the user presses it, the program will direct the user to the about page (which is just a page of text).
The error happened on the About.class, Eclipse said that there is no such class called About. And I am not quite understand why there is an About.class in the intent argument as well....
And idea??

you have class About.java in org.example.sudoku package?
public class About extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}

Did you write down your activity in the manifest, using the right or same package as your other activity?
http://developer.android.com/guide/topics/manifest/manifest-intro.html
Could you also please post your log cat?

as per your comment ........ You must have the file About.java in you project and also add it's entry in the manifest...
<activity android:name=".About"
android:label="#string/app_name">
</activity>
so add About.java in parallel to Sudoku.java in your project and it also need to extends the Activity.........

Related

assign button press to evoke a new page from sliding menu

I've implemented a sliding menu android framework by following a tutorial video, the menu works well but I can't figure out how to make the buttons generate new "fresh", i.e. distinct, pages wherein I can place subsequent components/"activities".
Right now all the buttons do is "toggle" back and forth between having the menu exposed, and hiding the menu, making visible the complete "landing page".
This is how it looks:
In my layout file, I think this is responsible for assigning functionality to the buttons:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 3" />
So as you can see the buttons evoke the method toggleMenu which looks like this:
public void toggleMenu() {
switch (this.menuCurrentState){
case CLOSED:
this.menuCurrentState = MenuState.OPENING;
this.menu.setVisibility(View.VISIBLE);
this.menuAnimatonScroller.startScroll(0, 0, this.getMenuWidth(),
0, menuAnimationDuration);
break;
case OPEN:
this.menuCurrentState = MenuState.CLOSING;
this.menuAnimatonScroller.startScroll(this.currentContentOffset,
0, -this.currentContentOffset, 0, menuAnimationDuration);
break;
default:
return;
}
this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable, menuAnimationPollingInterval);
this.invalidate();
}
I guess I need to make a new method for generating fresh pages and then assign that to the button push rather than toggle menu, I've tried this a few times using intent but I've not been able to quite figure it out.
What do I need to account for in such an operation?
How should such a function look?
The complete code can be found on my github page.
Thank you for your consideration.
dig this tutorial, it's one of the only helpful ones, in my opinion:
http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
or check the pertinent info below"
XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml – Represent screen 1
res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
File : res/layout/main2.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
2. Activities
Create two activity classes :
AppActivity.java –> main.xml
App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
Intent intent = new Intent(context, anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
File : App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>

Android project display error on R.id.button

I surdenly noticed that My project starts throwing error anytime I try to access a resources that is a button. It underlines R.id.button. I dont understand why. I even deleted the last xml that I created but problem persist.
This is an example of my xml file
<?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:background="#drawable/layoutborder"
android:orientation="vertical" >
<TextView
android:id="#+id/chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/stepone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/wine" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#drawable/ai" />
<Button
android:id="#+id/drugdetails"
style="#style/smallButtonStyleBlackpearl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="#string/nextbut" />
</LinearLayout>
My Java code
package com.example.rhemahealthcare;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.rhemahealthcare.R;
public class SteponeActivity extends SherlockActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.steponeactivity);
final Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class);
startActivity(intent);
}
});
}
}
i think you change any button1 id buy clicking right click and choose edit id. this option changes all the ids with that name in all the layouts.
As #Aleks G gussed it right in the comment, you don't have any button with id as button1 in your xml file. You've mentioned it :
final Button button = (Button)findViewById(R.id.button1);
Use the appropriate ID or put one in your layout file.
I have figured out the problem. My button ids were automatically change to button1 so they did not reference their previous ids that I gave to them. Thanks alot

Android — Add new tab when button click like Google Chrome new button?

In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?
First, is it possible in Android?
If possible, how to do this?
When I click on the + button, a new tab should be generated. How to do this?
Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.
Your Manifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dynamictab"
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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BlankActivity"/>
</application>
</manifest>
here is your activity_main.xml file for Tab activity
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_weight="1">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip" />
</HorizontalScrollView>
<Button android:id="#+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Add"/>
</LinearLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.
Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_event"
android:clickable="true"
android:focusable="true"
android:text="Default Tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is your MainActivity.java file
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab();
((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabIndex++;
addTab();
}
});
}
private void addTab(){
LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);
Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
protected void setupTab(View tabBtn, Intent setClass,String tag) {
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
tabHost.addTab(setContent);
}
}
And here is the BlankActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BlankActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(BlankActivity.this);
tv.setText("Blank activity");
setContentView(tv);
}
}
For button click create a new tabspec.
TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
//What ever thing you want to display inside the tab
TextView text = new TextView(CONTEXT);
text.setText("New tab");
return(text);
}
});
spec.setIndicator("New");
th.addTab(spec);
Use the following youtube tutorial as reference. Its simple.
http://www.youtube.com/watch?v=NcKSFlYEqYY
Create an Activity as below:
public class WebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView wv=new WebView(this);
setContentView(wv);
}
}
Now use #Partik's method to add a new tab, on each + button click:
private void addTab(){
Button tabBtn = new Button(MainActivity.this);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, WebViewActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}

Can i display the CallLog numbers in my edittext? New here

I have created a simple xml with one button and one edittext.
Upon clicking the button, i can get into the CallLog page.
Is it possible to display the selected numbers into my EditText, after i click on any numbers in my CallLog??
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/edittext"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_weight="0.29"
android:hint="CONTACT NUMBER" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL LOG" />
</LinearLayout>
Coding:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CallLogRetrieveActivity extends Activity {
Button myCallLogBtn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myCallLogBtn=(Button)findViewById(R.id.btn);
myCallLogBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(myIntent);
}});
}
}
You should start reading manual and tutorial first, not asking here about elementary things that you'd simply know after doing RTFM. So "yes, you can". But now please do your homework, by reading "android call log tutorial" googled materials. Nobody here is going that for you.

Error "android (name of project) has stopped unexpectedly

package com.iperetz1.android.testbutton1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestButton extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button test2 = (Button)findViewById(R.id.test2);
test2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
setContentView(R.layout.test2);;
}
});
Button other = (Button)findViewById(R.id.backmain);
other.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
setContentView(R.layout.main);;
}
});
}
}
main.xls
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="#+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2"
android:layout_x="24px"
android:layout_y="165px"
>
</Button>
</AbsoluteLayout>
test2.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="#+id/backmain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="backmain"
android:layout_x="24px"
android:layout_y="165px"
>
</Button>
</AbsoluteLayout>
findViewById is a lot simpler than people tend to think it is. It traverses the view hierarchy looking for a view with the given ID. If it's not found, findViewById returns null.
You started by setting the content view to your main layout but later on you tried to findViewById(R.id.backmain). Since there is no view with that ID in your main layout, it returns null. At that point attempting other.setOnClickListener will fail. You will only be able to do this when your button actually exists in the view hierarchy.
There's nothing inherently wrong with dynamically changing your view hierarchy, but you'll have to handle some things differently if you go that route. (Such as when you wire up events to views that don't exist during onCreate like you're trying to do above.)
As #Cristian Castiblanco said, changing the view dynamically is causing the problem, for these kind of scenarios, you have to create separate activities and invoke them using intents and pass data between them using bundles.

Categories

Resources