Make ListFragment open a Activity - android

Oke so i'm really new to android studio and android app dev in general but i wanted to make a app that my youth movement could use as sort of a database but i ran into a problem.
So i have a drawer with all the groups and if you press the drawer buttons it opens a ListFragment with names and what i want to happen is when i press the name a activity shows up with the name a photo and a phone number. But i can't seem to figure out how to open a activity from that listFragment.
Here is the ListFragment.java
package app.jarifile.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by User on 23/09/2015.
*/
public class Givers_Fragment extends ListFragment {
Intent i;
String[] listitems = {
"(Takleider)",
"(Takleider)",
"(Leider)",
"(Leider)"
};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listitems));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id){
switch(position){
case 0:
i = new Intent(getActivity(), Test_Activity.class);
break;
case 1:
i = new Intent(getActivity(), Test_Activity.class);
break;
case 2:
i = new Intent(getActivity(), Test_Activity.class);
break;
}
startActivity(i);
}
And this is the Test_Activity code.
package app.jarifile.test;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by User on 24/09/2015.
*/
public class Test_Activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
}
This is what logcat says
09-28 18:48:28.915 24081-24081/app.jarifile.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.jarifile.test, PID: 24081
android.content.ActivityNotFoundException: Unable to find explicit activity class {app.jarifile.test/app.jarifile.test.Test_Activity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1793)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1515)
at android.app.Activity.startActivityForResult(Activity.java:4026)
at android.app.Activity.startActivityForResult(Activity.java:3973)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
at android.app.Activity.startActivity(Activity.java:4297)
at android.app.Activity.startActivity(Activity.java:4265)
at app.jarifile.test.Givers_Fragment.onListItemClick(Givers_Fragment.java:51)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1544)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3721)
at android.widget.AbsListView$3.run(AbsListView.java:5660)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
09-28 18:48:31.635 24081-24081/app.jarifile.test I/Process﹕ Sending signal. PID: 24081 SIG: 9
Thank you for the help.

You have not declared Test_Activity in your AndroidManifest.xml. All Activities in your app must be declared there. Here's an example of what that might look like:
<manifest ... >
<application ... >
<activity android:name=".Test_Activity" />
...
</application ... >
...
</manifest>
http://developer.android.com/guide/components/activities.html#Declaring

try to replace startActivity(i) with getActivity().startActivity(i)

Related

Android studio creating a settings page

I am slightly new to Android studio and have the following code for a settings page using a fragment. I used a settingsFragment and settingsActivity to create the settings page. I also created a button in the mainActivity that should take me to the settings page. But whenever I run it, the app just closes. Im not sure how to fix this. Could somebody please take a look?
MainActivity.java:
package com.example.settingspagecs309;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openSettingsActivity();
}
});
}
public void openSettingsActivity(){
Intent intent=new Intent(this,SettingsActivity.class);
startActivity(intent);
}
}
ActivityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open settings"/>
</RelativeLayout>
SettingsFragment.java:
package com.example.settingspagecs309;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences,rootKey);
Preference myPref= (Preference) findPreference("Contact_Preference");
myPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
startActivity(new Intent(getContext(),MainActivity.class));
return true;
}
});
}
}
SettingsActivity.java:
package com.example.settingspagecs309;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();
ActionBar menu=getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setDisplayShowHomeEnabled(true);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
This is the error message it is showing:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.settingspagecs309, PID: 29462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.settingspagecs309/com.example.settingspagecs309.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.preference.Preference.setOnPreferenceClickListener(androidx.preference.Preference$OnPreferenceClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.preference.Preference.setOnPreferenceClickListener(androidx.preference.Preference$OnPreferenceClickListener)' on a null object reference
at com.example.settingspagecs309.SettingsFragment.onCreatePreferences(SettingsFragment.java:22)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:228)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1277)
at android.app.Activity.performStart(Activity.java:6306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 29462 SIG: 9

Having issues in moving from one activity to another using a button in Andriod Studio

I'm trying to create a sound board app with multiple actives with their own buttons and sounds. I cant seem to get the hang of moving from one activity to another using a button. The first activity moves just fine to the second, but the second doesn't move to the third and suddenly closes the app. Any help is greatly appreciated.
Here is my code for the first activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnMove;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMove=(Button)findViewById(R.id.btnMove);
btnMove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent a = new Intent(MainActivity.this,Main2Activity.class);
startActivity(a);
finish();
}
});
}
}
Here is my code for the second activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
Button btnMove2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnMove2=(Button)findViewById(R.id.btnMove2);
btnMove2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent b = newIntent(Main2Activity.this,Main3Activity.class);
startActivity(b);
finish();
}
});
}
}
What you have shown appears to work fine (albeit that using finish() is not what would normally be done). However you haven't shown the 3rd activity and that is likely where the issue is. It is also highly likely, assuming that exactly the same happened when finish() was removed from both MainActivity and Main2Activity that the App is crashing. It would be nearly impossible to determine why without the log and the code for the 3rd activity (including the layout XML, which may or may not be relevant).
One issue that would result in something similar to what you describe, is if you haven't added the 3rd activity to the manifest. This would result in an unexpected error and the App crashing with something along the lines of:
06-09 08:35:01.628 23844-23844/mjt.so44446139 E/AndroidRuntime: FATAL EXCEPTION: main
Process: mjt.so44446139, PID: 23844
android.content.ActivityNotFoundException: Unable to find explicit activity class {mjt.so44446139/mjt.so44446139.Activity3}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at mjt.so44446139.Activity2$1.onClick(Activity2.java:23)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
The above indicating an ActivityNotFoundException. If this is the case then you need to add Main3Activity to the manifest (you would have done this for the 2nd Activity).
Regarding finish()
If you have finish() coded after you start an activity via startActivity, then that activity will not be returned to as it has been finished. It will return to instead to that activities parent, if there is one.
Thus:
If finish() is in the first activity then that activity will not be returned to (clicking the devices back button from 2nd activity will finish the App).
If finish() is ONLY in the 2nd activity then it will not be returned to from the 3rd activity. However, returning from the 3rd (e.g. clicking the device's back button) will return to the 1st activity.
If finish() is in both the first and 2nd activity then neither will be returned to, thus clicking the device's back button will finish the App.
The normal use of finish() is to code it when you are done with an activity (not moving to another). Hence in the 3rd activity there is a line that resets the button's text to done. If finish() is un-commented then this will act to finish the activity and return to the 2nd (assuming it hasn't been finished).
Example Code
The following code may assist you in understanding or perhaps even fixing your issue (it should resemble your code quite closely):
It consists of 3 activities all with a button and currently with finish() commented out. They all use the same layout, which includes a Button and a TextView. The Textview is set to reflect the activity's name. As it is, it should reflect what you appear to want. That is clicking the button from the MainActivity takes you to the second. Clicking the button on the 2nd Activity takes you to the third (clicking on the button in the third does nothing).
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView msg = (TextView) this.findViewById(R.id.message);
msg.setText(this.getClass().getSimpleName());
Button nextbutton = (Button) this.findViewById(R.id.nextbutton);
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Activity2.class);
startActivity(intent);
//finish();
}
});
}
}
Activity2
public class Activity2 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button nextbutton = (Button) this.findViewById(R.id.nextbutton);
TextView msg = (TextView) this.findViewById(R.id.message);
msg.setText(this.getClass().getSimpleName());
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this,Activity3.class);
startActivity(intent);
//finish();
}
});
}
}
Activity3
public class Activity3 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button nextbutton = (Button) this.findViewById(R.id.nextbutton);
nextbutton.setText("Done");
TextView msg = (TextView) this.findViewById(R.id.message);
msg.setText(this.getClass().getSimpleName());
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//finish();
}
});
}
}
The layout used for all 3 is (noting that tools:context="mjt.so44446139.MainActivity" would have to be changed, this is the default Blank Activity layout with the button added and with an id given to the TextView):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mjt.so44446139.MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/nextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</android.support.constraint.ConstraintLayout>
The manifest is (main thing is that both Activity2 and Activity3 have been added):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mjt.so44446139">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2">
</activity>
<activity android:name=".Activity3">
</activity>
</application>
</manifest>

#android:id/list cannot resolve symbol

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
/**
* Created by name on 3/13/17.
* Used for the specials tab to allow a list of days to choose what special one would like to view
*/
public class SpecialsScroll extends ListFragment implements AdapterView.OnItemClickListener{
public SpecialsScroll(){
//Empty Constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.fragment_specials_scroll, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getActivity(), R.array.days, android.R.layout.simple_list_item_1)); //Loads array into the ListFragment
getListView().setOnItemClickListener(this); //activates the listener for this Fragment class
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){ // For checking when a user taps on an option
SpecialsPage specialsPage = new SpecialsPage();
Bundle foodArgs = new Bundle();
switch (position){ // checks what option is chosen and sends respective keys as parameters
case 0:
int[] keysMonday = {R.string.boom_boom_enchiladas_key, R.string.tacos_mexican_key};
foodArgs.putIntArray("keys", keysMonday);
break;
case 1:
int[] keysTuesday = {R.string.shrimp_avocado_tacos_key, R.string.green_chile_chicken_enchiladas_key};
foodArgs.putIntArray("keys", keysTuesday);
break;
}
specialsPage.setArguments(foodArgs);
getActivity().getFragmentManager().beginTransaction().replace(R.id.container, specialsPage).addToBackStack(null).commit();
}
}
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if(getFragmentManager().findFragmentByTag("CURRENT") == null){
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
switch (item.getItemId()) {
case R.id.special:
getFragmentManager().beginTransaction().replace(R.id.container, new SpecialsScroll(), "CURRENT").commit();
return true;
case R.id.locations:
getFragmentManager().beginTransaction().replace(R.id.container, new LocationsScroll(), "CURRENT").commit();
return true;
case R.id.order:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
}
So, I am trying to create a ListView and everything in the code works, but suddenly I am unable to get the xml to recognize the android:id/list attribute for some reason, I don't know why. When I click on an item in the listview in my app it still does what it should, I just really want to get rid of the error.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<TextView
android:id="#+id/text_specialsscroll"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
04-26 14:06:30.299 2576-2576
/com.namename.www.name
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namename.www.name, PID: 2576
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.namename.www.name/com.namename.www.name.MainActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ListFragment.ensureList(ListFragment.java:402)
at android.app.ListFragment.onViewCreated(ListFragment.java:203)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.BackStackRecord.run(BackStackRecord.java:816)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentController.execPendingActions(FragmentController.java:371)
at android.app.Activity.performStart(Activity.java:6695)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Change your id to this:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

Send multiple images form sdcard in android using Environment.getExternalStoragePublicDirectory functon

below find the java file i dont no where it gone wrong but my app is unfortunately stopping ple can some one help me where and in which line i am going wrong.....
below is my logcat ... details
FATAL EXCEPTION: main
Process: com.example.ankita.implicitintent, PID: 16822
java.lang.NullPointerException
at com.example.ankita.implicitintent.MainActivity.onClick(MainActivity.java:82)
at android.view.View.performClick(View.java:4651)
at android.view.View$PerformClick.run(View.java:19310)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
06-27 17:17:40.025 16822-16822/com.example.ankita.implicitintent I/Process: Sending signal. PID: 16822 SIG: 9
package com.example.ankita.implicitintent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button imgssend;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgssend=(Button)findViewById(R.id.images_id);
imgssend.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
Intent i=null,chooser=null;
switch (view.getId())
{
case R.id.images_id:
File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listofPictures=Pictures.list();
Uri uri=null;
ArrayList<Uri> arraylist=new ArrayList<Uri>();
for(String picture:listofPictures)
{
uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
arraylist.add(uri);
}
i=new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM,arraylist);
chooser=Intent.createChooser(i,"Send multiple");
startActivity(chooser);
break;
}
}
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.ankita.implicitintent.MainActivity">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/send_images"
android:id="#+id/images_id"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Looks Like Something is wrong with the OnClick Override. Why dont you use the onClick Listener of the Button?
Something like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imgssend=(Button)findViewById(R.id.images_id);
imgssend.setOnClickListener(new OnClickListener{
#Override
public void onClick(View v) {
Intent i=null,chooser=null;
File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listofPictures=Pictures.list();
Uri uri=null;
ArrayList<Uri> arraylist=new ArrayList<Uri>();
for(String picture:listofPictures)
{
uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
arraylist.add(uri);
}
i=new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM,arraylist);
chooser=Intent.createChooser(i,"Send multiple");
startActivity(chooser);
}
});
}
}

Android Studio: Activity not found exception when using Intents in ALL programming projects

After picking up my projects after a month, I run into a really catastrophic problem: I cannot start new activities anymore! Everytime I use an intent to do that, I get an Activity not found error and Android studio wants me to declare my activities in the manifest. However, I have checked the declarations for what feels like years now and have no idea where the problem is. I have created a (several, rather) new project(s) from scratch to avoid any complications and the problem persists. Among other things, I have tried declaring the activities like this
android:name=".MainActivity"
and like this
android:name="com.example.dorothea.intenttest.MainActivity"
and formulating the intent like this
public final Intent intenttest = new Intent(this, SecondActivity.class);
and this
public final Intent intenttest = new Intent(MainActivity.this, SecondActivity.class);
Also, because this problem persists in even the simplest of projects, I cannot think of a null pointer exception in the construction of the started activity that has escaped me. I am really stumped here and any help would be very appreciated. Probably it is a really dumb error on my part, but I cannot see which one :(
In the following, I attach the logcat and code of a very simple (two blank pages) app that I used to find the problem. I let Android Studio create two blank activities and just added two lines of code to the Java file of the first.
Logcat:
08-24 13:36:32.460 26726-26726/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dorothea.intenttest/com.example.dorothea.intenttest .MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$700(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.dorothea.intenttest.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
at android.app.Activity.startActivityForResult(Activity.java:3434)
at android.app.Activity.startActivityForResult(Activity.java:3395)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:3630)
at android.app.Activity.startActivity(Activity.java:3598)
at com.example.dorothea.intenttest.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$700(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5302)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
08-24 13:36:39.137 26726-26726/? I/Process﹕ Sending signal. PID: 26726 SIG: 9
Java file first activity:
package com.example.dorothea.intenttest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public final Intent intenttest = new Intent(this, SecondActivity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(intenttest);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The activity to be started is just like android created it:
package com.example.dorothea.intenttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And needless to say, Android Studio has in fact declared the activities in the manifest file, which is here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dorothea.intenttest" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
You can't use context before it's initialized during activity lifecycle. Intents can be created after onCreate()
Move this line sowhere inside listener or some method called when activity is running (create it just before use):
public final Intent intenttest = new Intent(this, SecondActivity.class);
Error is in this line of code
public final Intent intenttest = new Intent(this, SecondActivity.class);
You have to move that initialization into onCreate or some other activity method.
You have to pass Activity instance to the Intent constructor and that instance has not yet been created.
you shoud use your package to declare activity in manifest. In your case :
<activity
android:name=".intenttest.SecondActivity"
android:label="#string/title_activity_second" >

Categories

Resources