0

我是 android 开发的新手,我一直在创建一个包含两个 tabs 的应用程序。在一个选项卡中将显示一个包含图像的网格,而在另一个选项卡上将显示另一个页面。我创建了 3 个 XML 布局文件和 3 个 java 类文件,其代码如下。(稍后我将编写 pageActivity.java 的代码)

MainActivity.java

public class MainActivity extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabhost = getTabHost();

    TabSpec imgspec = tabhost.newTabSpec("Images");
    imgspec.setIndicator("Images");
    Intent imgIntent = new Intent(this,ImageAdapter.class);
    imgspec.setContent(imgIntent);

    TabSpec pagespec = tabhost.newTabSpec("NewPage");
    pagespec.setIndicator("New Page");
    Intent pageIntent = new Intent(this,pageActivity.class);
    pagespec.setContent(pageIntent);

    tabhost.addTab(imgspec);
    tabhost.addTab(pagespec);
}

}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
Context mContext;

public Integer[] mThumbIds  = {R.drawable.ic_action_search , R.drawable.ic_launcher , R.drawable.img1 };

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount(){
    return mThumbIds.length ; 
}

public Object getItem(int position){

    return mThumbIds[position];
}

public View getView(int position , View convertView , ViewGroup parent){
     ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;


}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}

pageActivity.java

public class pageActivity extends Activity {

}    

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>

    </LinearLayout>

</TabHost>

img_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</GridView>

page_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


</LinearLayout>

在运行时,应用程序崩溃。查看 LogCat 后,似乎问题出在 ImageAdapter.java 文件上。请帮忙 。

LogCat 错误如下所示

08-27 19:16:14.772: E/AndroidRuntime(390): FATAL EXCEPTION: main
08-27 19:16:14.772: E/AndroidRuntime(390): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prac1/com.example.prac1.MainActivity}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prac1/com.example.prac1.ImageAdapter}: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.os.Looper.loop(Looper.java:130)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.reflect.Method.invoke(Method.java:507)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-27 19:16:14.772: E/AndroidRuntime(390):  at dalvik.system.NativeStart.main(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prac1/com.example.prac1.ImageAdapter}: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.widget.TabHost.addTab(TabHost.java:216)
08-27 19:16:14.772: E/AndroidRuntime(390):  at com.example.prac1.MainActivity.onCreate(MainActivity.java:32)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-27 19:16:14.772: E/AndroidRuntime(390):  ... 11 more
08-27 19:16:14.772: E/AndroidRuntime(390): Caused by: java.lang.InstantiationException: com.example.prac1.ImageAdapter
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.Class.newInstanceImpl(Native Method)
08-27 19:16:14.772: E/AndroidRuntime(390):  at java.lang.Class.newInstance(Class.java:1409)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-27 19:16:14.772: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-27 19:16:14.772: E/AndroidRuntime(390):  ... 20 more
4

2 回答 2

0

你的问题是它ImageAdapter不是一个Activity,因此Android不知道如何实例化它,就好像它是一个一样。

您需要创建一个Activity包含您的ImageAdapter和在该Activity集合中的适配器在其GridView.

首先,将 img_layout.xml 更改为:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img_grid"
android:layout_width="match_parent"
android:layout_height="match_parent" >

让我们调用你的新 Activity ImageGridActivity。在你的范围内ImageGridActivity.onCreate()你会做:

setContentView(R.id.img_layout);
GridView grid = (GridView)findViewById(R.id.img_grid);
grid.setAdapter(new ImageAdapter(this));
于 2012-08-27T14:14:08.493 回答
0

您正在尝试使用 设置其中一个选项卡ImageAdapter,但ImageAdapter不是Activity. 您只能将 Activity 类设置为选项卡。所以删除下面的代码

Intent imgIntent = new Intent(this,ImageAdapter.class);
imgspec.setContent(imgIntent);
于 2012-08-27T14:07:05.167 回答