Java 意图不起作用。第二个活动启动错误
我正在android studio中构建一个应用程序。在启动主活动时,当我单击以查看列表项的详细信息时,第二个活动不会启动。我已在名为Onlistitemclick的方法中的popuplistfragment.java中编写了意图,并在滑动视图中的oncreate方法中指定了捆绑包。您能提供帮助吗Java 意图不起作用。第二个活动启动错误,java,android-intent,android-studio,android-activity,Java,Android Intent,Android Studio,Android Activity,我正在android studio中构建一个应用程序。在启动主活动时,当我单击以查看列表项的详细信息时,第二个活动不会启动。我已在名为Onlistitemclick的方法中的popuplistfragment.java中编写了意图,并在滑动视图中的oncreate方法中指定了捆绑包。您能提供帮助吗 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle save
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public class PopupListFragment extends ListFragment implements View.OnClickListener {
//String pageData[];
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
for (int i = 0, z = Cheeses.QUOTES.length ; i < z ; i++) {
items.add(Cheeses.QUOTES[i]);
}
setListAdapter(new PopupAdapter(items));
}
@Override
public void onListItemClick(ListView listView, View v, int position, long id) {
String item = (String) listView.getItemAtPosition(position);
Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);
myIntent.putExtra("key", item);
startActivity(myIntent);
// Show a toast if the user clicks on an item
// Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
}
/**
* A simple array adapter that creates a list of cheeses.
*/
class PopupAdapter extends ArrayAdapter<String> {
PopupAdapter(ArrayList<String> items) {
super(getActivity(), R.layout.list_item, android.R.id.text1, items);
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
// Let ArrayAdapter inflate the layout and set the text
View view = super.getView(position, convertView, container);
return view;
}
}
}
public class SwipeViews extends Activity {
String pageData[]; //Stores the text to swipe.
LayoutInflater inflater; //Used to create individual pages
ViewPager vp; //Reference to class to swipe views
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.swipe_view);
//Get the data to be swiped through
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
String item = extras.getString("key");
textview.setText(item);
}
pageData=getResources().getStringArray(R.array.quotes);
//get an inflater to be used to create single pages
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Reference ViewPager defined in activity
vp=(ViewPager)findViewById(R.id.viewPager);
//set the adapter that will create the individual pages
vp.setAdapter(new MyPagesAdapter());
}
//Implement PagerAdapter Class to handle individual page creation
class MyPagesAdapter extends PagerAdapter {
@Override
public int getCount() {
//Return total pages, here one for each data item
return pageData.length;
}
//Create the given page (indicated by position)
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = inflater.inflate(R.layout.page, null);
((TextView)page.findViewById(R.id.textMessage)).setText(pageData[position]);
//Add the page to the front of the queue
((ViewPager) container).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//See if object from instantiateItem is related to the given view
//required by API
return arg0==(View)arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
object=null;
}
}
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
公共类PopupListFragment扩展ListFragment实现View.OnClickListener{
//字符串pageData[];
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayList items=新建ArrayList();
for(inti=0,z=Cheeses.QUOTES.length;i
这是清单文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreen"
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=".MainActivity"
android:label="SuccessQuotes" >
<intent-filter>
<action android:name="com.hacktechbd.successquotes.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SwipeViews"
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>
确保您能够检测到单击事件。 使用类似
Log.v("check","item is clicked");
在你的代码里
及
尝试使用
Intent myIntent=newintent(getActivity(),SwipeViews.class)代码>
而不是
Intent myIntent=newintent(PopupListFragment.this.getActivity(),SwipeViews.class)代码>
它可能会工作。确保您能够检测到单击事件。
使用类似
Log.v("check","item is clicked");
在你的代码里
及
尝试使用
Intent myIntent=newintent(getActivity(),SwipeViews.class)代码>
而不是
Intent myIntent=newintent(PopupListFragment.this.getActivity(),SwipeViews.class)代码>
它可能会工作。尝试在MainActivity类中进行导航!也发布错误日志。您的SwipeView类正在扩展活动,而不是AppCompatActivity或FragmentActivity。看看这是否不是问题所在。当我将SwipeView类作为单独的应用程序启动以在MainActivity类中进行导航时,它工作正常!也发布错误日志。您的SwipeView类正在扩展活动,而不是AppCompatActivity或FragmentActivity。看看这是否不是问题所在。我的SwipeView类作为单独的应用程序启动时工作正常