I'm completely new to java, so i have started making an app to find the TOTAL by presing add and subtract buttons. I have defined buttons and the text view in FRAGMENT_MAIN.XML, and their functions in main activity.JAVA . After defining the functions of buttons in main activity.java, I'm getting logcat errors in my program and the app is getting unfortunately stoped in android emulator. i am using android sdk version 22.6.4, eclipse ide for java develpers version is 2.0.2.20140224-0000 and eclipse platform version is 4.3.2.v2.140221-1852.
my MainActivity.java
package com.counterehr.abhi;
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and FRAGMENT_MAIN.XML is
FRAGMENT_MAIN.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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.counterehr.abhi.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="Add one"
android:textSize="20sp" />
<Button
android:id="#+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bAdd"
android:layout_centerVertical="true"
android:text="Subtract one"
android:textSize="20sp" />
</RelativeLayout>
on running the application i am getting logcat errors as.
logcat errors
07-01 18:16:17.963: E/Trace(833): error opening trace file: No such file or directory (2)
07-01 18:16:19.675: D/dalvikvm(833): GC_FOR_ALLOC freed 80K, 9% free 2419K/2644K, paused 356ms, total 356ms
07-01 18:16:19.704: I/dalvikvm-heap(833): Grow heap (frag case) to 3.415MB for 960016-byte allocation
07-01 18:16:19.802: D/dalvikvm(833): GC_FOR_ALLOC freed 1K, 7% free 3355K/3584K, paused 97ms, total 97ms
07-01 18:16:20.002: D/dalvikvm(833): GC_CONCURRENT freed <1K, 7% free 3355K/3584K, paused 9ms+33ms, total 201ms
07-01 18:16:20.382: D/dalvikvm(833): GC_CONCURRENT freed <1K, 6% free 3779K/4004K, paused 5ms+4ms, total 52ms
07-01 18:16:20.543: D/AndroidRuntime(833): Shutting down VM
07-01 18:16:20.543: W/dalvikvm(833): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-01 18:16:20.583: E/AndroidRuntime(833): FATAL EXCEPTION: main
07-01 18:16:20.583: E/AndroidRuntime(833): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.counterehr.abhi/com.counterehr.abhi.MainActivity}: java.lang.NullPointerException
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.os.Looper.loop(Looper.java:137)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-01 18:16:20.583: E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 18:16:20.583: E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 18:16:20.583: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-01 18:16:20.583: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-01 18:16:20.583: E/AndroidRuntime(833): at dalvik.system.NativeStart.main(Native Method)
07-01 18:16:20.583: E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
07-01 18:16:20.583: E/AndroidRuntime(833): at com.counterehr.abhi.MainActivity.onCreate(MainActivity.java:33)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.Activity.performCreate(Activity.java:5104)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-01 18:16:20.583: E/AndroidRuntime(833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-01 18:16:20.583: E/AndroidRuntime(833): ... 11 more
07-01 18:16:23.873: I/Process(833): Sending signal. PID: 833 SIG: 9
please! someone help me out of this, i tried every possible thing but not getting the solution.
The problem is that you are mixing up Fragments and Activities, and as such, handling events in the Activity, even though these Views are displayed in the Fragment. Basically you are meant to have a FrameLayout in the Activity, and that's it - the logic is handled in the Fragment.
An example is the following:
public class ContainerActivity extends ActionBarActivity
{
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
this.setContentView(R.layout.activity_container);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (saveInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_container_container, new ExampleFragment())
.addToBackStack(null)
.commit();
}
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount == 0)
{
finish();
}
}
});
}
}
activity_container.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/activity_container_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ExampleFragment:
public class ExampleFragment extends Fragment implements View.OnClickListener
{
private Button btnOne;
private Button btnTwo;
private Button btnThree;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_example, container, false);
btnOne = (Button) rootView.findViewById(R.id.example_button_one);
btnTwo = (Button) rootView.findViewById(R.id.example_button_two);
btnThree = (Button) rootView.findViewById(R.id.example_button_three);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v)
{
if (btnOne == v)
{
Toast.makeText(getActivity(), "One.", Toast.LENGTH_LONG).show();
}
else if (btnTwo == v)
{
Toast.makeText(getActivity(), "Two.", Toast.LENGTH_LONG).show();
}
else if (btnThree == v)
{
Toast.makeText(getActivity(), "Three.", Toast.LENGTH_LONG).show();
}
}
}
fragment_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/example_button_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/hello"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<Button
android:id="#+id/example_button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_button_one"
android:layout_alignRight="#+id/example_button_one"
android:layout_below="#+id/example_button_one"
android:layout_marginTop="30dp"
android:text="#string/hello" />
<Button
android:id="#+id/example_button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_button_two"
android:layout_alignRight="#+id/example_button_two"
android:layout_below="#+id/example_button_two"
android:layout_marginTop="30dp"
android:text="#string/hello" />
</RelativeLayout>
Android-Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
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.ContainerActivity"
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>
And that should be a valid example, it shows how you can use an Activity to display a Fragment, and handle events in that Fragment. It currently doesn't show how the Fragment communicates with the Activity to change to another Fragment, which is by the way done by an interface, which you would make a variable to and store in the onAttach() callback method.
But I did include that as well in the "mildly refined" edition on NullPointerException accessing views in onCreate()
Related
I have a layout with few labels and textviews which I want to update based on a action. For testing I have set up the java MainActivity to load the textviews I want to use on created.
Once I click a button called add, the code is executed and prints that the add button was pushed.
I also want the text area to say add for testing. Later on in the project I will change what is displayed here.
However, once I click the button the program stops. I know that the button works because the print shows up in the log.
I have looked and looked and all I can find is the textfield.settext(); But when using this the error occurs and the program stops.
Below is my XML for the TextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000066"
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.smartshop.MainActivity$PlaceholderFragment" >
<LinearLayout
android1:id="#+id/linearLayout1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:orientation="vertical" >
<ListView
android1:id="#+id/cartlist"
android1:layout_width="289dp"
android1:layout_height="236dp"
android1:textColor="#color/textc" >
</ListView>
<LinearLayout
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<Button
android1:id="#+id/btnadd"
android1:layout_width="0pt"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:background="#color/buttonc"
android1:onClick="clkaddbutton"
android1:text="Add Item" />
<!-- android1:onClick="clkaddbutton" -->
<android.support.v7.widget.Space
android1:id="#+id/space1"
android1:layout_width="2dp"
android1:layout_height="wrap_content" />
<Button
android1:id="#+id/btnremove"
android1:layout_width="0pt"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:background="#color/buttonc"
android1:onClick="clkremovebutton"
android1:text="Remove Item" />
<!-- android1:onClick = "clkremovebutton" -->
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="#dimen/verticalspacesize">
</View>
<LinearLayout
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<TextView
android1:id="#+id/lblsubtotal"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="3.40"
android1:text="Sub Total"
android1:textSize="#dimen/textsize"
android1:textColor="#color/textc" />
<TextView
android1:id="#+id/tvsubtotal"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="3.40"
android1:textSize="#dimen/textsize"
android1:text="TextView" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="#dimen/verticalspacesize">
</View>
<LinearLayout
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<TextView
android1:id="#+id/lbltax"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:text="Tax"
android1:textSize="#dimen/textsize"
android1:textColor="#color/textc" />
<TextView
android1:id="#+id/tvtax"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:textSize="#dimen/textsize"
android1:text="TextView" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="#dimen/verticalspacesize">
</View>
<LinearLayout
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<TextView
android1:id="#+id/lbltotal"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:text="Total"
android1:textSize="#dimen/textsize"
android1:textColor="#color/textc" />
<TextView
android1:id="#+id/tvtotal"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_weight="1"
android1:textSize="#dimen/textsize"
android1:text="TextView" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the java code in the MainActivity:
package com.example.smartshop;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import java.lang.*;
public class MainActivity extends ActionBarActivity {
private Button btnadd;
private Button btnremove;
private TextView tfsubtotal ;
private TextView tftax;
private TextView tftotal;
public void clkaddbutton(View view) {
System.out.println("Add clicked");
tfsubtotal.setText("add");
}
public void clkremovebutton(View view) {
tfsubtotal.setText("Rem");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnremove = (Button) findViewById(R.id.btnremove);
btnadd = (Button) findViewById(R.id.btnadd);
tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
if(tfsubtotal != null){
System.out.println("The subtotal edittext was created");
tfsubtotal.setText("test" );
}
else{
System.out.println("The textview is null");
}
//tfsubtotal.setText("Google is your friend." );
tftax = (TextView) findViewById(R.id.tvtax);
tftotal = (TextView) findViewById(R.id.tvtotal);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I know that the problem is on the tfsubtotal.setText("add"); line because I have tested without it and everything works.
I have also tried to use edit text boxes instead of textviews but I get the same error.
Can anyone suggest what I might be doing wrong?
Thank you.
Below is the android log:
05-22 22:37:21.251: I/System.out(1693): The subtotal edittext was created
05-22 22:37:21.251: D/AndroidRuntime(1693): Shutting down VM
05-22 22:37:21.261: W/dalvikvm(1693): threadid=1: thread exiting with uncaught exception (group=0xb2accba8)
05-22 22:37:21.271: E/AndroidRuntime(1693): FATAL EXCEPTION: main
05-22 22:37:21.271: E/AndroidRuntime(1693): Process: com.example.smartshop, PID: 1693
05-22 22:37:21.271: E/AndroidRuntime(1693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smartshop/com.example.smartshop.MainActivity}: java.lang.NullPointerException
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.os.Handler.dispatchMessage(Handler.java:102)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.os.Looper.loop(Looper.java:136)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 22:37:21.271: E/AndroidRuntime(1693): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 22:37:21.271: E/AndroidRuntime(1693): at java.lang.reflect.Method.invoke(Method.java:515)
05-22 22:37:21.271: E/AndroidRuntime(1693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 22:37:21.271: E/AndroidRuntime(1693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 22:37:21.271: E/AndroidRuntime(1693): at dalvik.system.NativeStart.main(Native Method)
05-22 22:37:21.271: E/AndroidRuntime(1693): Caused by: java.lang.NullPointerException
05-22 22:37:21.271: E/AndroidRuntime(1693): at com.example.smartshop.MainActivity.onCreate(MainActivity.java:42)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.Activity.performCreate(Activity.java:5231)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-22 22:37:21.271: E/AndroidRuntime(1693): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-22 22:37:21.271: E/AndroidRuntime(1693): ... 11 more
05-22 22:37:23.451: I/Process(1693): Sending signal. PID: 1693 SIG: 9
Try this:
public class MainActivity extends ActionBarActivity {
private Button btnadd;
private Button btnremove;
private TextView tfsubtotal ;
private TextView tftax;
private TextView tftotal;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnadd = (Button) findViewById(R.id.btnadd);
System.out.println("The subtotal edittext was created");
//tfsubtotal.setText("Google is your friend." );
tftax = (TextView) findViewById(R.id.tvtax);
TextView tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
tftotal = (TextView) findViewById(R.id.tvtotal);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void clkaddbutton(View view) {
// tfsubtotal.
System.out.println("Add clicked");
tfsubtotal.setText("add");
}
public void clkremovebutton(View view) {
tfsubtotal.setText("Rem");
}
OR
Why not just do simply like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnadd = (Button) findViewById(R.id.btnadd);
Button btnRemove = (Button)findViewById(R.id.btnremove);
tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
System.out.println("The subtotal edittext was created");
//tfsubtotal.setText("Google is your friend." );
tftax = (TextView) findViewById(R.id.tvtax);
tftotal = (TextView) findViewById(R.id.tvtotal);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
btnadd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
System.out.println("Add clicked");
tfsubtotal.setText("add");
}
});
btnRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
System.out.println("Add clicked");
tfsubtotal.setText("Rem");
}
});
}
I am learning android programming using eclipse. I coded a twitter app that simply let's you tweet a status and view a user's timeline. However when I try run the app on my samsung galaxy tab 3.0 the app starts up, all i see is the title at the top, none of the textviews or buttons show up then after 2 seconds the screen goes black and i get the error message ("Unfortunately TwitterApp has stopped") I cant imagine it's something to do with twitter since none of the widgets are being displayed. Here is the code from my main activity, and the activity_main.xml file. And the logCat. Ill even include the manifest
package com.example.twitterapp;
import java.util.List;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
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 {
TextView countCharsTV, timelineTV;
EditText usernameET, tweetET;
Button tweetBTN, timelineBTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countCharsTV = (TextView) findViewById(R.id.textView1);
timelineTV = (TextView) findViewById(R.id.textView2);
timelineTV.setMovementMethod(new ScrollingMovementMethod());
tweetET = (EditText) findViewById(R.id.editText1);
tweetET.addTextChangedListener(new MyTextWatcher());
usernameET = (EditText) findViewById(R.id.editText2);
tweetBTN = (Button) findViewById(R.id.button1);
tweetBTN.setOnClickListener(tweetBTNOnClickListener);
timelineBTN.setOnClickListener(timelineBTNOnClickListener);
}
//BUTTON CLICK LISTENERS WITHOUT IMPLEMENTS
public OnClickListener tweetBTNOnClickListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
new MyAsyncTaskTweet().execute(tweetET.getText().toString());
}
};
public OnClickListener timelineBTNOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
new MyAsyncTaskTimeline().execute(timelineTV.getText().toString());
}
};
//END OF BUTTON CLICK LISTENERS
//COUNT CHARS IN TEXTVIEW USING IMPLEMENTS
class MyTextWatcher implements TextWatcher {
#Override
public void afterTextChanged(Editable arg0) {
countCharsTV.setText("" + tweetET.getText().length());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
}
//End OF COUNT CHARS IN TEXTVIEW
//ASYNC TASK CLASSES
//TWEET ASYNC TASK
public class MyAsyncTaskTweet extends AsyncTask<String,Void,String> {
#Override
protected String doInBackground(String... tweet) {
String result = "";
Twitter twitter = TwitterFactory.getSingleton();
try{
twitter.updateStatus(tweet[0]);
result = "Success";
}catch(TwitterException twitterException) {
result = "Failed to update status";
}
return result;
}
#Override
protected void onPostExecute(String result){
tweetET.setHint(result);
tweetET.setText("");
}
}
//END OF TWEET ASYNC TASK
public class MyAsyncTaskTimeline extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... username) {
String result = new String("");
List<twitter4j.Status> statuses = null;
Twitter twitter = TwitterFactory.getSingleton();
try {
statuses = twitter.getUserTimeline(username[0]);
}catch (TwitterException twitterException) {
twitterException.printStackTrace();
}
for(twitter4j.Status status : statuses) {
result += status.getText();
result += "\n";
}
return result;
}
#Override
protected void onPostExecute(String result) {
usernameET.setText("");
timelineTV.setText(result);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:text="Tweet" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Type your tweet here" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_toLeftOf="#+id/editText2"
android:text="'#" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_toRightOf="#+id/textView1"
android:text="Timeline" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10"
android:hint="Type a username here" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_toRightOf="#+id/button1"
android:text="Timeline will go here" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twitterapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.twitterapp.MainActivity"
android:label="#string/app_name" >
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat
03-21 22:46:25.117: W/ActivityThread(3806): Application com.example.twitterapp can be
debugged on port 8100...
03-21 22:46:25.289: E/SensorManager(3806): thread start
03-21 22:46:25.296: D/SensorManager(3806): registerListener :: handle = 1598182229
name= BOSCH BMC150 Acceleration Sensor delay= 200000
03-21 22:46:25.296: D/AndroidRuntime(3806): Shutting down VM
03-21 22:46:25.296: W/dalvikvm(3806): threadid=1: thread exiting with uncaught exception
(group=0x40dcd2a0)
03-21 22:46:25.304: E/AndroidRuntime(3806): FATAL EXCEPTION: main
03-21 22:46:25.304: E/AndroidRuntime(3806): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.twitterapp/com.example.twitterapp.MainActivity}:
java.lang.NullPointerException
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.access$700(ActivityThread.java:146)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.os.Looper.loop(Looper.java:137)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.main(ActivityThread.java:4949)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
java.lang.reflect.Method.invokeNative(Native Method)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
java.lang.reflect.Method.invoke(Method.java:511)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
dalvik.system.NativeStart.main(Native Method)
03-21 22:46:25.304: E/AndroidRuntime(3806): Caused by: java.lang.NullPointerException
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.example.twitterapp.MainActivity.onCreate(MainActivity.java:45)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.Activity.performCreate(Activity.java:5185)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
03-21 22:46:25.304: E/AndroidRuntime(3806): ... 11 more
03-21 22:46:38.398: I/Process(3806): Sending signal. PID: 3806 SIG: 9
Where is timelineBTN reference? "timelineBTN" was null.
timelineBTN = (Button)findViewById(R.id.);
I am new to android app development( from yesterday only i started to learn android ).
I am getting message in AVD like - unfortunately has stopped ,please help me solve this error, I want learn more in android but sucked by this error.. plz..plz.. help me to sove this.
my MainActivity is
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mb=(ImageButton) findViewById(R.id.imageButton1);
ImageButton mc=(ImageButton) findViewById(R.id.imageButton2);
ImageButton me=(ImageButton) findViewById(R.id.imageButton3);
ImageButton mf=(ImageButton) findViewById(R.id.imageButton4);
ImageButton mg=(ImageButton) findViewById(R.id.imageButton5);
ImageButton mh=(ImageButton) findViewById(R.id.imageButton6);
mb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent activityChangeIntent= new Intent(MainActivity.this,Second.class);
MainActivity.this.startActivity(activityChangeIntent);
}
});
mc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
me.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
}
}
My MainActivity XML is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg1"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*" >
<TableRow>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/mob1" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/cam1" />
</TableRow>
<TableRow >
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/comp" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/watch1" />
</TableRow>
<TableRow >
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/abt1" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/contact1" />
</TableRow>
</TableLayout>
</LinearLayout>
MY Logcat is :
11-04 05:14:36.971: E/AndroidRuntime(1088): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.somayaji.abhi/com.somayaji.abhi.Second}; have you declared this activity in your AndroidManifest.xml?
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivityForResult(Activity.java:3423)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivityForResult(Activity.java:3384)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivity(Activity.java:3626)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivity(Activity.java:3594)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.somayaji.abhi.MainActivity$1.onClick(MainActivity.java:40)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.view.View.performClick(View.java:4424)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.view.View$PerformClick.run(View.java:18383)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Handler.handleCallback(Handler.java:733)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Looper.loop(Looper.java:137)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-04 05:14:36.971: E/AndroidRuntime(1088): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 05:14:36.971: E/AndroidRuntime(1088): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-04 05:14:36.971: E/AndroidRuntime(1088): at dalvik.system.NativeStart.main(Native Method)
11-04 05:14:49.691: I/Process(1088): Sending signal. PID: 1088 SIG: 9
my Second activity is
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mpage);
}
}
My second activity XML is :
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/TextView" />
</LinearLayout>
plz.. explain why am getting like this.. tell me the solution
Straight from logcat :
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.somayaji.abhi/com.somayaji.abhi.Second}; have you declared this activity in your AndroidManifest.xml?
You have forgotten to declare your Second activity in your manifest xml file.
Try to read the logcat, messages are often very informative and you'll be able to correct your error faster than post it here.
This is an ActivityNotFound Exception.
include
<activity android:name="com.somayaji.abhi.Second" >
</activity>
inside Application tag of your Manifest file.
Declare the Second activity
<activity android:name="com.somayaji.abhi.Second" >
</activity>
in manifest file
Check the topic Add it to manifest
http://developer.android.com/training/basics/firstapp/starting-activity.html
I am trying to use the tabhost when I add it to my xml it doesn't look right, and I believe there is something that needs to be done in java, I am trying to set up three tabs with three different classes is this possible?
Here is my 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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
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:text="Browser History:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Log"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Messages"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
And I have three different classes, because I am trying to use each tab to open each activity.
Here are the Classes
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TabHost;
import android.widget.TextView;
public class AndroidSpybetaActivity extends TabActivity {
/** Called when the activity is first created. */
Resources res;
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, Messaging.class);
spec = tabHost.newTabSpec("messaging").setIndicator("Messaging")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestingData.class);
spec = tabHost.newTabSpec("Calls").setIndicator("Calls")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
TextView view = (TextView) findViewById(R.id.hello);
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
null, null, null, null);
mCur.moveToFirst();
int index = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
while (mCur.isAfterLast() == false) {
view.append(" WebSite " + mCur.getString(index));
mCur.moveToNext();
}
}
}
Second
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Messaging extends TabActivity{
static TextView messageBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageBox = (TextView) findViewById(R.id.tvSms);
}
public static void updateMessageBox(String msg) {
messageBox.append(msg);
}
}
and third
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.TextView;
public class TestingData extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.call);
String[] projection = new String[] {
Calls.NUMBER
};
Cursor mCur = managedQuery(CallLog.Calls.CONTENT_URI, projection,
Calls.DURATION + "<?", new String[] { "60" }, Calls.DURATION
+ " ASC");
mCur.moveToFirst();
while (mCur.isAfterLast() == false) {
for (int i = 0; i < mCur.getColumnCount(); i++) {
view.append(" Number " + mCur.getString(i));
}
mCur.moveToNext();
}
}
}
Here is my logcat:
08-12 15:19:16.368: D/AndroidRuntime(280): Shutting down VM
08-12 15:19:16.368: W/dalvikvm(280): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-12 15:19:16.628: E/AndroidRuntime(280): FATAL EXCEPTION: main
08-12 15:19:16.628: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.AndroidSpybetaActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-12 15:19:16.628: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:2473)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.addTab(TabHost.java:213)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.johnnydicamillo.spybeta.AndroidSpybetaActivity.onCreate(AndroidSpybetaActivity.java:31)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-12 15:19:16.628: E/AndroidRuntime(280): ... 11 more
08-12 15:19:21.929: I/Process(280): Sending signal. PID: 280 SIG: 9
Yes, it is possible.
You can specify each activity (Start an intent) for each tabs in the following manner
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, CalendarActivity.class);
spec = tabHost.newTabSpec("calendar").setIndicator("Calendar", res.getDrawable(R.drawable.ic_tab_calendar)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProfileActivity.class);
spec = tabHost.newTabSpec("profile").setIndicator("Profile", res.getDrawable(R.drawable.ic_tab_profile)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
Each activity will have their own content layout views, therefore no need to worry about that in the main layout.
Your main XML layout will be small and simple as
<?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="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
I think this is what you want.
It is not possible to have a tab correspond to an activity. The purpose of tabs is to break up one activity into how ever many views if there is too much info display in one view(not to be confused with activity). Here however is how you set up TabHost:
First start with an xml:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
-
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button" />
</FrameLayout>
</LinearLayout>
</TabHost>
Sorry that's hard to read but if you copy it into eclipse you should be fine. And then the Java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabDemo extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs=(TabHost)findViewById(R.id.tabhost); //Id of tab host
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");//make a new tab
spec.setContent(R.id.tab1); //What is in the tab (not an activity but rather a view)
spec.setIndicator("Clock"); //Name of tab
tabs.addTab(spec); //Add it
spec=tabs.newTabSpec("tag2"); //Same thing here
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
}
}
This is relatively simple an I am sure there is support in the android development website and of course just searching on Google. This code was copied out of Beginning Android 4 by Grant Allen and the book explains this topic in much greater detail. Good luck!
I have an app that needs to capture a customer's signature and then present it in an ImageView. For proof-of-concept, I've written a small app that has a layout with a GestureOverlayView at the top, then a "Save Signature" button, then an ImageView below. When I press the Save Signature button, it should create an image from the GestureOverlayView and display it in the ImageView. Below is the code/layout and then a list of issues:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView android:id="#+id/GestureView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF"
android:fadeEnabled="false"
android:gestureStrokeType="multiple"
android:gestureStrokeWidth="1.5"
android:gestureColor="#000000"
android:fadeOffset="600000"
android:eventsInterceptionEnabled="false"
android:alwaysDrawnWithCache="true">
</android.gesture.GestureOverlayView>
<Button android:id="#+id/SubmitButton"
android:text="Save Signature"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageView android:id="#+id/ImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF">
</ImageView>
</LinearLayout>
Code:
public class TestActivity extends Activity {
ImageView iv;
GestureOverlayView gv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.ImageView);
gv = (GestureOverlayView)findViewById(R.id.GestureView);
Button submitButton = (Button)findViewById(R.id.SubmitButton);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
}
});
}
}
I get a Null Pointer exception every time on the createBitmap() method. I believe it is because gv.getDrawingCaches() is returning null. As you can see, drawing cache IS enabled so it should return SOMETHING!
Here's the logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
at com.pgov.barcode.TestActivity$2.onClick(TestActivity.java:42)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Any and all help would be greatly appreciated!
Apparently I answered my own question. Even though I used:
android:alwaysDrawnWithCache="true"
I changed the code to:
gv.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
gv.setDrawingCacheEnabled(false);
And it works! Not sure why the XML flag doesn't affect it though...