繁体   English   中英

意图无效。第二个活动启动错误

[英]Intent not working.Second activity launching error

我正在android studio中构建一个应用程序。当我单击以查看第二个活动未启动的列表项的详细信息时启动主要活动。滑动视图中的oncreate方法。可以提供帮助吗?

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;
    }
}

}

这是清单文件。

<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 = new Intent(getActivity(), SwipeViews.class);

而不是Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);

它可能会奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM