2013-11-17 4 views
3

У меня есть GridView, как показано здесь:OnCreate() не вызывается

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rl_drag_and_drop_app" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<SlidingDrawer 
    android:id="@+id/bottom" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:orientation="horizontal" 
    android:layout_marginTop="200dp" 
    android:content="@+id/content" 
    android:handle="@+id/handle" > 

    <Button 
     android:id="@+id/handle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/arrow" /> 

    <LinearLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="#00FF00"> 

      <!-- Editext for Search --> 
<EditText android:id="@+id/inputSearch" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/Search_applications" 
    android:inputType="textVisiblePassword"/> 

<ListView 
    android:id="@+id/lvApps" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
/> 
    </LinearLayout> 
</SlidingDrawer> 

     <Button 
     android:id="@+id/btnLinkToPersonalize" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:background="@null" 
     android:text="@string/Personalize" 
     android:textColor="#21dbd4" 
     android:textStyle="bold" /> 

<GridView 
android:id="@+id/GRIDVIEW1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="30dip" 
android:numColumns="auto_fit" 
android:columnWidth="60dp" 
android:horizontalSpacing="10dp" 
android:verticalSpacing="10dp" 
android:gravity="center" 
android:stretchMode="columnWidth" 
> 

</GridView> 

<ImageView 
    android:id="@+id/trash_can" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/trashcanDescription_Delete" 
    android:padding="40dip" 
    android:src="@drawable/trashcan" 
    android:visibility="gone" > 
</ImageView> 

</RelativeLayout> 

А затем кодирования, установленный в моем GridView.java:

public class GridView extends Activity {  
private int draggedIndex = -1; 
private BaseAdapter adapter; 
ArrayList<Integer> drawables = new ArrayList<Integer>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.drag_and_drop_app); 
    Log.d("GridView", "onCreate called"); 
    drawables.add(R.drawable.pattern1); 
    drawables.add(R.drawable.pattern2); 

android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1); 

    // Instance of Adapter Class 
    gridView.setAdapter(new GridViewAdapter(this)); 

} 

А затем адаптер в другом классе здесь:

package com.example.awesomefilebuilderwidget; 
IMPORTS 

public class GridViewAdapter extends BaseAdapter { 
private Context mContextGV; 

// Keep all Images in array list 
public ArrayList<Integer> drawables = new ArrayList<Integer>(); 

// Constructor 
public GridViewAdapter(Context c){ 
    mContextGV = c; 
} 

@Override 
// How many items are in the data set represented by this Adapter 
public int getCount() { 
    return drawables.size(); 
} 

@Override 
// Get the data item associated with the specified position in the 
// data set 
public Object getItem(int position) { 
    return drawables.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Try to reuse the views 
    ImageView view = (ImageView) convertView; 
    // if convert view is null then create a new instance else reuse it 
    if (view == null) { 
     view = new ImageView(mContextGV); 
    } 

    view.setImageResource(drawables.get(position)); 
    view.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70)); 
    view.setTag(String.valueOf(position)); 
    return view; 
} 

} 

И мой Maniest установлен как так:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.awesomefilebuilderwidget" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
android:minSdkVersion="8" 
android:targetSdkVersion="18" /> 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<application 
android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@style/AppTheme" > 

<receiver android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="@string/app_name"> 
<intent-filter> 
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
</intent-filter> 

<meta-data android:name="android.appwidget.provider" 
android:resource="@xml/widget_stuff"/> 

</receiver> 

<activity android:name="com.example.awesomefilebuilderwidget.WidgetConfig" android:label="@string/app_name"> 
<intent-filter> 
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/> 

</intent-filter> 

</activity> 

<activity android:name="com.example.awesomefilebuilderwidget.Drag_and_Drop_App" android:label="@string/app_name" android:windowSoftInputMode="stateHidden" android:screenOrientation="portrait"></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.AppInfoAdapter" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.Feedback" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.GridView" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.SendMessageActivity" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.Utilities" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.Personalize" android:label="@string/app_name" ></activity> 
<activity android:name="com.example.awesomefilebuilderwidget.SwipeDetector" android:label="@string/app_name"></activity> 

</application> 

</manifest> 

Однако, когда я запускаю свою программу и перехожу к макету Drag_and_Drop_App (макет, показанный выше), onCreate gridView никогда не вызывается, и поэтому изображения никогда не появляются. Я дважды проверил все и не смог понять, почему он не появляется.

Пожалуйста, помогите!

ответ

0

Необходимо указать способ запуска деятельности GridView. Нет никаких указаний на то, как вы начали работу GridView.

+0

Я не знал об этом. Не могли бы вы объяснить, что вы имеете в виду? – user2909006

+0

После регистрации действия в файле манифеста вы должны начать эту деятельность, используя startActivity где-то в вашем коде. – saiful103a

+0

Могу ли я использовать startActivity в моем методе onCreate() моего класса Drag_and_Drop_App, чтобы он запускал gridView? – user2909006

0

если GridView является первой активностью вашего приложения, вы должны добавить фильтр намерений в манифесте декларации:

<activity android:name="com.example.awesomefilebuilderwidget.GridView"    android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 
+0

Поскольку мое приложение настроено как приложение appWidget, это не сработало. Я пробовал это как в моей работе GridView, так и в активности, которую включил gridView, и это не сработало – user2909006

Смежные вопросы