大家好,我是刚接触android应用程序的人,我有一个问题,我不明白为什么mDemo = (SliderLayout)findviewById(R.id.slider)的这部分代码为什么出错?

是因为我没有在SliderLayout下导入任何内容吗?

这是我的代码

package com.example.imageslider;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.imageslider.SliderLayout;

import java.util.HashMap;

public class MainActivity extends Activity {

    private SliderLayout mDemoSlider;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDemoSlider = (SliderLayout)findViewById(R.id.slider);

    HashMap<String,String> url_maps = new HashMap<String, String>();
}
}


XML格式

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:custom="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:context="com.example.MainActivity" >
 <com.daimajia.slider.library.SliderLayout
    android:id="@+id/slider"
    android:layout_width="match_parent"
    custom:pager_animation="Accordion"
    custom:auto_cycle="true"
    custom:indicator_visibility="visible"
    custom:pager_animation_span="1100"
    android:layout_height="200dp" />

<com.daimajia.slider.library.Indicators.PagerIndicator
    android:id="@+id/custom_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    custom:selected_color="#0095BF"
    custom:unselected_color="#55333333"
    custom:selected_drawable="@string/hello_world"
    custom:shape="oval"
    custom_selected_drawable="@string/hello_world"
    custom:selected_padding_left="5dp"
    custom:selected_padding_right="5dp"
    custom:unselected_padding_left="5dp"
    custom:unselected_padding_right="5dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    custom:selected_width="6dp"
    custom:selected_height="6dp"
    custom:unselected_width="6dp"
    custom:unselected_height="6dp"
    android:layout_marginBottom="20dp"
     />

 <ListView
    android:layout_below="@+id/slider"
    android:id="@+id/transformers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>


 </RelativeLayout>


SliderLayout
    软件包com.daimajia.slider.library;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class SliderLayout extends RelativeLayout {

public SliderLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context);
    // TODO Auto-generated constructor stub
}

}


谢谢,我将此网站用作参考资料https://github.com/daimajia/AndroidImageSlider

最佳答案

在您的代码中,

import com.example.imageslider.SliderLayout;


但是在您的XML中

<com.daimajia.slider.library.SliderLayout


因此,当您转换视图R.id.slider时,它的类型错误。尝试将导入行更改为

import com.daimajia.slider.library.SliderLayout;

关于java - 图像滑块错误Android应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27261965/

10-09 00:19