java.lang.RuntimeException: Unable to start activity - java

i'm trying to study how to make apps for Android.
Following a tutorial made by Google with my modification i get this error
12-26 10:42:23.848: D/dalvikvm(1302): GC_CONCURRENT freed 362K, 15% free 2737K/3216K, paused 70ms+89ms, total 204ms
12-26 10:42:23.898: D/AndroidRuntime(1302): Shutting down VM
12-26 10:42:23.898: W/dalvikvm(1302): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-26 10:42:23.928: E/AndroidRuntime(1302): FATAL EXCEPTION: main
12-26 10:42:23.928: E/AndroidRuntime(1302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.startactivity/com.example.startactivity.DisplayMessageActivity}: java.lang.NullPointerException
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1234)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.os.Looper.loop(Looper.java:137)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 10:42:23.928: E/AndroidRuntime(1302): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 10:42:23.928: E/AndroidRuntime(1302): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 10:42:23.928: E/AndroidRuntime(1302): at dalvik.system.NativeStart.main(Native Method)
12-26 10:42:23.928: E/AndroidRuntime(1302): Caused by: java.lang.NullPointerException
12-26 10:42:23.928: E/AndroidRuntime(1302): at com.example.startactivity.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:20)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.Activity.performCreate(Activity.java:5104)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 10:42:23.928: E/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 10:42:23.928: E/AndroidRuntime(1302): ... 11 more
12-26 10:43:11.418: I/Process(1302): Sending signal. PID: 1302 SIG: 9
this is the code of MainActivity
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.startactivity.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText edit_nome = (EditText) findViewById(R.id.edit_nome);
EditText edit_cognome = (EditText) findViewById(R.id.edit_cognome);
Bundle bundle = new Bundle();
bundle.putString("nome", edit_nome.getText().toString());
bundle.putString("cognome", edit_cognome.getText().toString());
intent.putExtra(EXTRA_MESSAGE, bundle);
startActivity(intent);
}
}
And this is the code of DisplayMessageActivity
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String nome = intent.getBundleExtra("nome").toString();
String cognome = intent.getBundleExtra("cognome").toString();
TextView textNome = new TextView(this);
TextView textCognome = new TextView(this);
textNome.setTextSize(40);
textCognome.setTextSize(40);
textNome.setText(nome);
textCognome.setText(cognome);
setContentView(textNome);
setContentView(textCognome);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for help.

You are calling to the toString method of the Bundle instead of retrieving the string stored on it. In addition, you are trying to get a Bundle with a key that is stored into it. Try chaning
String nome = intent.getBundleExtra("nome").toString();
String cognome = intent.getBundleExtra("cognome").toString();
to
Bundle bundle = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE);
if (bundle != null) {
String nome = bundle.getString("nome");
String cognome = bundle.getString("cognome");
}
Take in consideration that if the intent received is not your, and it comes with no Bundle, or a different one, your nome and cognome strings will be null, not empty strings.

The logcat shows the problem at-
com.example.startactivity/com.example.startactivity.DisplayMessageActivity
So, the best guess is the nome and cognome values might be null at the point where you set the text in the texviews in the DisplayMessageActivity
Try adding a debug point at the line with text
String nome = intent.getBundleExtra("nome").toString();
and see for any null values.
Hope this jelps

Change MainActivity code to
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.startactivity.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText edit_nome = (EditText) findViewById(R.id.edit_nome);
EditText edit_cognome = (EditText) findViewById(R.id.edit_cognome);
Bundle bundle = new Bundle();
bundle.putString("nome", edit_nome.getText().toString());
bundle.putString("cognome", edit_cognome.getText().toString());
intent.putExtra(EXTRA_MESSAGE, bundle);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Make sure that entry in manifest file:
<activity android:name=".DisplayMessageActivity"></activity>

Related

Issue with fragment OptionsMenu when recall fragment?

MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new LayOutOne()).commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.settings:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new PrefFragment())
.addToBackStack(null)
.commit();
break;
}
return super.onOptionsItemSelected(item);
}
PrefFragmentList extends PreferenceFragment
public class PrefFragmentList extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
PrefFragment extends Fragment
public class PrefFragment extends Fragment {
private View v;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = inflater.inflate(R.layout.layout_settings, null);
setHasOptionsMenu(true);
return v;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
menu.findItem(R.id.settings).setVisible(true).setEnabled(false).setChecked(true).setChecked(true);
super.onCreateOptionsMenu(menu, inflater);
}
PROBLEM:
MainActivity call PrefFragment from OptionMenu , when i go back to MainActivity and re-call PrefFragment, the application crash.
LOGCAT:
11-27 12:12:50.857 1387-1387/xx.xxx.myapplication D/dalvikvm: GC_FOR_ALLOC freed 228K, 3% free 9211K/9476K, paused 25ms, total 25ms
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication D/AndroidRuntime: Shutting down VM
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4178b700)
11-27 12:13:02.592 1387-1387/xx.xxxx.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at xx.xxx.myapplication.PrefActivity.onCreateView(PrefActivity.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment
at android.app.Activity.onCreateView(Activity.java:4751)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at xxx.xxxx.myapplication.PrefFragment.onCreateView(PrefFragment.java:23) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
I try replace in OnCreateView:
final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
with:
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.layout_settings, container, false);
} catch (InflateException e) {
}
return rootView;
Work fine and resolve this error:
LOGCAT
From the logcat: Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment

Progress Dialog, Broadcast receiver crash in android

I have been trying for a long time to work on a button onClick listener that performs the following
launch a loading screen (progress bar).
Send an sms msg in the background to a specific number.
wait for the reply using broadcast listener, which is a message containing a link.
dismiss the loading screen.
then take this link and use it to start another activity.
The code shows no errors, however each time I press the button it doesn't show the progress bar, it only sends a message then waits for the specified time then crashes.
Here's the code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
#Override
`public void onClick(View arg0) {
// TODO Auto-generated method stub
new LoadTracker().execute();
}});
}
protected void sendSMS() {
// TODO Auto-generated method stub
String Phoneno = "***********";
String Message = "www.google.com";
SmsManager manager= SmsManager.getDefault();
manager.sendTextMessage(Phoneno, null, Message, null, null);
}
private BroadcastReceiver Receiver = new BroadcastReceiver(){
public static final String SMS_BUNDLE = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(smsBody));
startActivity(in);
}
}
}};
private class LoadTracker extends AsyncTask<Void, Integer, Void>{
protected void onPreExecute(){
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("Sending Signal");
progressDialog.setMessage("Receiving Signal, Please Wait");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
synchronized (this){
IntentFilter IF = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
sendSMS();
registerReceiver(Receiver,IF);
this.wait(10000);
this.wait(10000);
publishProgress(100);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... values){
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(Void result)
{
//close the progress dialog
progressDialog.dismiss();
//initialize the View
setContentView(R.layout.activity_main);
}
}
#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
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(Receiver);
}
#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);
}
}
Note: The Phone number belongs to the phone on which the application is running so it should open google after it receives the reply
All the uses permissions are in place too.
Update: I discovered the problem with the progress dialog, the "O" letter in OnPreExecute Should've been lower case. Now the progress Dialog appears and the app sends the message however, the app crashes as soon as the reply sms message is received. How can I solve this?
Logcat:
04-20 19:45:45.950: W/dalvikvm(15877): threadid=1: thread exiting with uncaught exception (group=0x40c4b1f8)
04-20 19:45:45.974: E/AndroidRuntime(15877): FATAL EXCEPTION: main
04-20 19:45:45.974: E/AndroidRuntime(15877): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x10 (has extras) } in com.example.finalprototype.MainActivity$1#4162c988
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:741)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.os.Handler.handleCallback(Handler.java:605)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.os.Looper.loop(Looper.java:137)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.ActivityThread.main(ActivityThread.java:4512)
04-20 19:45:45.974: E/AndroidRuntime(15877): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:45:45.974: E/AndroidRuntime(15877): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 19:45:45.974: E/AndroidRuntime(15877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
04-20 19:45:45.974: E/AndroidRuntime(15877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
04-20 19:45:45.974: E/AndroidRuntime(15877): at dalvik.system.NativeStart.main(Native Method)
04-20 19:45:45.974: E/AndroidRuntime(15877): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com }
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1535)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1387)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.Activity.startActivityForResult(Activity.java:3190)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.Activity.startActivity(Activity.java:3297)
04-20 19:45:45.974: E/AndroidRuntime(15877): at com.example.finalprototype.MainActivity$1.onReceive(MainActivity.java:75)
04-20 19:45:45.974: E/AndroidRuntime(15877): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:732)
04-20 19:45:45.974: E/AndroidRuntime(15877): ... 9 more
04-20 19:45:56.013: I/Process(15877): Sending signal. PID: 15877 SIG: 9
Not sure but from the following line
ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com ...
I think your URL should be like http://www.google.com. Its missing the protocol http without which it might not recognize the right activity to handle the Intent.

Cant start a dialog activity from a button in another activity

I am trying to start a new activity as a Dialog Activity putting a Intent on a button from another activity. Whenever I try and press the button, the activity unfotunately closes.
Main Activity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ab = (Button) findViewById(R.id.button1);
ab.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, NewDialog.class);
startActivity(intent);
}
#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;
}
public void side(View view){
android.support.v4.app.FragmentManager fm1 = getSupportFragmentManager();
NewDialog newdialog = new NewDialog();
newdialog.show(fm1, "SideDialog");
}
#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.
*/
}
NewDialog.java
public class NewDialog extends DialogFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
final View view = inflater.inflate(R.layout.activity_new_dialog, container);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Added Logcat:
05-29 22:37:36.472: D/dalvikvm(26806): Late-enabling CheckJNI
05-29 22:37:36.762: D/ActivityThread(26806): setTargetHeapUtilization:0.25
05-29 22:37:36.762: D/ActivityThread(26806): setTargetHeapIdealFree:8388608
05-29 22:37:36.762: D/ActivityThread(26806): setTargetHeapConcurrentStart:2097152
05-29 22:37:39.172: D/libEGL(26806): loaded /system/lib/egl/libEGL_adreno200.so
05-29 22:37:39.222: D/libEGL(26806): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-29 22:37:39.322: D/libEGL(26806): loaded /system/lib/egl/libGLESv2_adreno200.so
05-29 22:37:39.362: I/Adreno200-EGL(26806): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)
05-29 22:37:39.362: I/Adreno200-EGL(26806): Build Date: 03/28/13 Thu
05-29 22:37:39.362: I/Adreno200-EGL(26806): Local Branch:
05-29 22:37:39.362: I/Adreno200-EGL(26806): Remote Branch: m/jb_rel_2.0.3.1
05-29 22:37:39.362: I/Adreno200-EGL(26806): Local Patches: NONE
05-29 22:37:39.362: I/Adreno200-EGL(26806): Reconstruct Branch: NOTHING
05-29 22:37:39.452: D/OpenGLRenderer(26806): Enabling debug mode 0
05-29 22:37:43.822: D/AndroidRuntime(26806): Shutting down VM
05-29 22:37:43.822: W/dalvikvm(26806): threadid=1: thread exiting with uncaught exception (group=0x40fd4438)
05-29 22:37:44.022: E/AndroidRuntime(26806): FATAL EXCEPTION: main
05-29 22:37:44.022: E/AndroidRuntime(26806): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.createdialog/com.example.createdialog.NewDialog}: java.lang.ClassCastException: com.example.createdialog.NewDialog cannot be cast to android.app.Activity
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.os.Looper.loop(Looper.java:137)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread.main(ActivityThread.java:4963)
05-29 22:37:44.022: E/AndroidRuntime(26806): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 22:37:44.022: E/AndroidRuntime(26806): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 22:37:44.022: E/AndroidRuntime(26806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-29 22:37:44.022: E/AndroidRuntime(26806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-29 22:37:44.022: E/AndroidRuntime(26806): at dalvik.system.NativeStart.main(Native Method)
05-29 22:37:44.022: E/AndroidRuntime(26806): Caused by: java.lang.ClassCastException: com.example.createdialog.NewDialog cannot be cast to android.app.Activity
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
05-29 22:37:44.022: E/AndroidRuntime(26806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
05-29 22:37:44.022: E/AndroidRuntime(26806): ... 11 more
I might be wrong here, but as far as I know, you can't start an Intent passing a DialogFragment, you should pass an Activity. Here's what's wrong:
Intent intent = new Intent(MainActivity.this, NewDialog.class);
startActivity(intent);
Since NewDialog is declared as a DialogFragment and not as an Activity subclass.
If what you are trying to do is show the dialog, then you should do:
NewDialog dialog = NewDialog();
// use getSupportFragmentManager() if using support fragments instead of native fragments, or
// use getChildFragmentManager() if this code is inside a fragment rather than an activity
dialog.show(getFragmentManager(), "pass-any-string-here");
Without a logcat stacktrace to look at, I am assuming that your issue has to do with variable scope (here is an example explanation on the topic).
What you need to do is declare your button as a class level variable because right now it is a method variable and as such ceases to exist once your onCreate method has finished executing.
public class MainActivity extends FragmentActivity {
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button1);
myButton.setOnClickListener(...);
...
}
}

Send Data from one Activity to another - not Working

I have a TO DO List, and when I check the checkbox in one Activity I want the CheckBox and the text that is next to the Checkbox to go to another Activity. this is my Adapter:
public class MyItemAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyItemAdapter(Context context, String[] values) {
super(context, R.layout.item_list, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_list, parent, false);
CheckBox textView = (CheckBox) rowView.findViewById(R.id.checkBox1);
textView.setText(values[position]);
return rowView;
}
public static SparseBooleanArray getCheckedItemPositions() {
// TODO Auto-generated method stub
return null;
}
public static void remove(Object pos) {
// TODO Auto-generated method stub
}
public void changeData(String[] items) {
// TODO Auto-generated method stub
}
public static void setAdapter(MyItemAdapter mAdapter) {
// TODO Auto-generated method stub
}
This is my First Activity code:
final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FinishedItems.class);
intent.putExtra("check", items);
Bundle extras = new Bundle();
extras.putString("status", "Data Recived");
intent.putExtras(extras);
startActivity(intent);
}
});
}
This is my Second Activity code:
Intent intent = getIntent();
String check = intent.getStringExtra("check");
((CheckBox)findViewById(R.id.checkBox1)).setText(check);
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();
And Finaly this is my Log cat:
02-18 10:33:24.779: D/AndroidRuntime(1575): Shutting down VM
02-18 10:33:24.779: W/dalvikvm(1575): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 10:33:24.789: E/AndroidRuntime(1575): FATAL EXCEPTION: main
02-18 10:33:24.789: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Stanton.quicktodolist/com.Stanton.quicktodolist.MainActivity}: java.lang.NullPointerException
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.os.Looper.loop(Looper.java:130)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 10:33:24.789: E/AndroidRuntime(1575): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 10:33:24.789: E/AndroidRuntime(1575): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 10:33:24.789: E/AndroidRuntime(1575): at dalvik.system.NativeStart.main(Native Method)
02-18 10:33:24.789: E/AndroidRuntime(1575): Caused by: java.lang.NullPointerException
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 10:33:24.789: E/AndroidRuntime(1575): ... 11 more
Please Help me
This line of your stacktrace
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)
points you to the error. Line 70 in the MainActivity causes a null pointer. It is often helpful to study and understand stacktraces. If you cannot figure it out yourself, show us the onCreate method of the MainActivity.
I believe that you replace the intent extra by the bundle extra just try this code and let me know pelase
final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FinishedItems.class);
intent.putExtra("check", items);
//Bundle extras = new Bundle();
// extras.putString("status", "Data Recived");
intent.putExtras("status", "Data Recived");
startActivity(intent);
}
});
}
And the int the second activity
Intent intent = getIntent();
String check = intent.getStringExtra("check");
((CheckBox)findViewById(R.id.checkBox1)).setText(check);
// Bundle bundle = intent.getExtras();
String status = intent.getStringExtra("status");
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();

How to access data in the previous activity?

Using StartActivity i reached SecondActivity from FirstActivity..Now i want to access object declared in the previous activity.
Is there any obvious mechanisms that i am missing.I cant use Parceable on the object(DropboxAPI) i want to pass since i do not have its source(i can't make it parceable).
Can i pass FirstActivity.this since i can make it Parceable using an intent?or like
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("MyClass",FirstActivity.this);
startActivity (intent);
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import android.app.Application;
import android.hardware.Camera.Parameters;
import android.os.Parcel;
import android.os.Parcelable;
public class temp extends Application {
DropboxAPI<AndroidAuthSession> mApi;
public void onCreate() {
super.onCreate();
}
temp(DropboxAPI<AndroidAuthSession> Api)
{
mApi=Api;
}
public DropboxAPI<AndroidAuthSession> getName() {
return mApi;
}
public void setName(DropboxAPI<AndroidAuthSession> dropboxclient) {
this.mApi = dropboxclient;
}
}
Code in first class:
t = (temp)getApplication();
t.setName(mApi);
//basket.putParcelable("key", MCActivity.this);
Intent intent=new Intent(MCActivity.this,DownActivity.class);
//intent.putExtra("MyClass",t);
startActivity (intent);
Code in Second class:
public void onCreate(Bundle bun) {
// TODO Auto-generated method stub
super.onCreate(bun);
setContentView(R.layout.download);
mImage = (ImageView)findViewById(R.id.image_view);
//Bundle gotit=getIntent().getExtras();
t = (temp)getApplication();
int i=2;
i=5;
dApi=t.getName();
}
Logcat:
06-28 02:24:31.979: I/System.out(729): debugger has settled (1308)
06-28 02:25:08.387: D/AndroidRuntime(729): Shutting down VM
06-28 02:25:08.387: W/dalvikvm(729): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 02:25:08.705: E/AndroidRuntime(729): FATAL EXCEPTION: main
06-28 02:25:08.705: E/AndroidRuntime(729): java.lang.ClassCastException: android.app.Application
06-28 02:25:08.705: E/AndroidRuntime(729): at cloud.mobile.MCActivity$3.onClick(MCActivity.java:164)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.view.View.performClick(View.java:2408)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.view.View$PerformClick.run(View.java:8816)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Handler.handleCallback(Handler.java:587)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Looper.loop(Looper.java:123)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 02:25:08.705: E/AndroidRuntime(729): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 02:25:08.705: E/AndroidRuntime(729): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 02:25:08.705: E/AndroidRuntime(729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 02:25:08.705: E/AndroidRuntime(729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 02:25:08.705: E/AndroidRuntime(729): at dalvik.system.NativeStart.main(Native Method)
try using Application class:
public class MyApplication extends Application {
//Variable we want to share to All Activities in Application
private DropboxClient dropboxclient;
#Override
public void onCreate() {
super.onCreate();
}
//Getter Method
public DropboxClient getName() {
return dropboxclient;
}
//Setter Method
public void setName(DropboxClient dropboxclient) {
this.dropboxclient = dropboxclient;
}
}
In MyFirstActivity Activity:
private MyApplication app;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (MyApplication)getApplication(); //Get Application
app.setName(dropboxclient);
Intent intent = new Intent();
intent.setClass(this, MySecondActivity.class);
startActivity(intent);
In MySecondActivity Activity:
private MyApplication app;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (MyApplication)getApplication(); //Get Application
DropboxClient dropboxclient=app.getName();//Access global value
do the following,
start the activity with startActivityForResult()
startActivityForResult(0, MyActivity.class);
before your started activity finished, in the started activity, create an intent, and put whatever data you want to pass back as extras in the intent. now call setResult(..., intent);
Intent resultIntent = new Intent();
resultIntent.putExtra("myKey", myVal);
setResult(0, resultIntent);
in your starting activity, implement onActivityResult(), and handle the result of the started activity. you will be passed an Intent, which is the intent where you added extras in the started activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //TODO handle here. 
}
Extend Application class and declare and initialize your cross activity objects in that class and write getters for those objects.
now using following code in an activty, you can access those Objects.
getApplication().getMyObject().doSomeAction();
remember, you will have to modifiy your androidManifest.xml to tell android about your extended application class.
Solution:
Using StartActivityforResult there data moveback to Second Activity to First Activity
One Example there data move to second Activity to first Activity-
http://micropilot.tistory.com/1577

Categories