我正在尝试使用此库:https://github.com/sephiroth74/HorizontalVariableListView
我从demo文件夹中复制并粘贴了SimpleHListActivity并复制了所有其他所需的文件,包括维度文件。但它仍然是破碎而且根本没有运行。
这是我做的:
SimpleHListActivity.java
import android.app.Activity;
import android.content.Context;
import android.support.v4.util.SparseArrayCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import it.sephiroth.android.library.util.v11.MultiChoiceModeListener;
import it.sephiroth.android.library.widget.HListView;
public class SimpleHListActivity extends Activity implements View.OnClickListener, it.sephiroth.android.library.widget.AdapterView.OnItemClickListener {
private static final String LOG_TAG = "SimpleHListActivity";
HListView listView;
Button mButton1;
Button mButton2;
Button mButton3;
TestAdapter mAdapter;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
List<String> items = new ArrayList<String>();
for( int i = 0; i < 50; i++ ) {
items.add( String.valueOf( i ) );
}
mAdapter = new TestAdapter( this, R.layout.test_item_1, android.R.id.text1, items );
listView.setHeaderDividersEnabled( true );
listView.setFooterDividersEnabled( true );
if( listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE_MODAL ) {
listView.setMultiChoiceModeListener( new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode( ActionMode mode, Menu menu ) {
return true;
}
@Override
public void onDestroyActionMode( ActionMode mode ) {
}
@Override
public boolean onCreateActionMode( ActionMode mode, Menu menu ) {
menu.add( 0, 0, 0, "Delete" );
return true;
}
@Override
public boolean onActionItemClicked( ActionMode mode, MenuItem item ) {
Log.d(LOG_TAG, "onActionItemClicked: " + item.getItemId());
final int itemId = item.getItemId();
if( itemId == 0 ) {
deleteSelectedItems();
}
mode.finish();
return false;
}
@Override
public void onItemCheckedStateChanged( ActionMode mode, int position, long id, boolean checked ) {
mode.setTitle( "What the...!" );
mode.setSubtitle( "Selected items: " + listView.getCheckedItemCount() );
}
} );
} else if( listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE ) {
listView.setOnItemClickListener(this);
}
listView.setAdapter( mAdapter );
mButton1.setOnClickListener( this );
mButton2.setOnClickListener( this );
mButton3.setOnClickListener( this );
Log.i( LOG_TAG, "choice mode: " + listView.getChoiceMode() );
}
@Override
public void onContentChanged() {
super.onContentChanged();
listView = (HListView) findViewById( R.id.hListView1 );
mButton1 = (Button) findViewById( R.id.button1 );
mButton2 = (Button) findViewById( R.id.button2 );
mButton3 = (Button) findViewById( R.id.button3 );
}
@Override
public void onClick( View v ) {
final int id = v.getId();
if( id == mButton1.getId() ) {
addElements();
} else if( id == mButton2.getId() ) {
removeElements();
} else if( id == mButton3.getId() ) {
scrollList();
}
}
private void scrollList() {
listView.smoothScrollBy( 1500, 300 );
}
private void addElements() {
for( int i = 0; i < 5; i++ ) {
mAdapter.mItems.add( Math.min( mAdapter.mItems.size(), 2), String.valueOf( mAdapter.mItems.size() ) );
}
mAdapter.notifyDataSetChanged();
}
private void removeElements() {
for( int i = 0; i < 5; i++ ) {
if( mAdapter.mItems.size() > 0 ) {
mAdapter.mItems.remove( Math.min( mAdapter.mItems.size()-1, 2 ) );
}
}
mAdapter.notifyDataSetChanged();
}
private void deleteSelectedItems() {
SparseArrayCompat<Boolean> checkedItems = listView.getCheckedItemPositions();
ArrayList<Integer> sorted = new ArrayList<Integer>( checkedItems.size() );
Log.i( LOG_TAG, "deleting: " + checkedItems.size() );
for( int i = 0; i < checkedItems.size(); i++ ) {
if( checkedItems.valueAt( i ) ) {
sorted.add( checkedItems.keyAt( i ) );
}
}
Collections.sort(sorted);
for( int i = sorted.size()-1; i >= 0; i-- ) {
int position = sorted.get( i );
Log.d( LOG_TAG, "Deleting item at: " + position );
mAdapter.mItems.remove( position );
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> adapterView, View view, int position, long l) {
Log.i( LOG_TAG, "onItemClick: " + position );
Log.d( LOG_TAG, "checked items: " + listView.getCheckedItemCount() );
Log.d( LOG_TAG, "checked positions: " + listView.getCheckedItemPositions() );
}
class TestAdapter extends ArrayAdapter<String> {
List<String> mItems;
LayoutInflater mInflater;
int mResource;
int mTextResId;
public TestAdapter( Context context, int resourceId, int textViewResourceId, List<String> objects ) {
super( context, resourceId, textViewResourceId, objects );
mInflater = LayoutInflater.from( context );
mResource = resourceId;
mTextResId = textViewResourceId;
mItems = objects;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public long getItemId( int position ) {
return getItem( position ).hashCode();
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType( int position ) {
return position%3;
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
if( null == convertView ) {
convertView = mInflater.inflate( mResource, parent, false );
}
TextView textView = (TextView) convertView.findViewById( mTextResId );
textView.setText( getItem( position ) );
int type = getItemViewType( position );
ViewGroup.LayoutParams params = convertView.getLayoutParams();
if( type == 0 ) {
params.width = getResources().getDimensionPixelSize( R.dimen.item_size_1 );
} else if( type == 1 ) {
params.width = getResources().getDimensionPixelSize( R.dimen.item_size_2 );
} else {
params.width = getResources().getDimensionPixelSize( R.dimen.item_size_3 );
}
return convertView;
}
}
}
activity_simple_hlist.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<it.sephiroth.android.library.widget.HListView
android:id="@+id/hListView1"
android:layout_width="match_parent"
android:layout_height="200dip"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:background="#11000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scroll" />
</LinearLayout>
test_item_1.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:background="@drawable/text_background_selector"
android:orientation="vertical"
android:gravity="center"
android:padding="5dip">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="centerInside"
android:adjustViewBounds="false" />
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
text_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@drawable/list_selector_background_selected" />
<item android:state_activated="true" android:drawable="@drawable/list_selector_background_selected" />
<item android:state_checked="true" android:drawable="@drawable/list_selector_background_selected" />
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/white" />
</selector>
LOGCAT:
01-27 15:53:31.150 4161-4161/com.shaheed.myapplication D/AndroidRuntime﹕ Shutting down VM
01-27 15:53:31.150 4161-4161/com.shaheed.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-27 15:53:31.160 4161-4161/com.shaheed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaheed.myapplication/com.shaheed.myapplication.SimpleHListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.shaheed.myapplication.SimpleHListActivity.onCreate(SimpleHListActivity.java:46)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
01-27 15:53:32.680 4161-4161/com.shaheed.myapplication I/Process﹕ Sending signal. PID: 4161 SIG: 9
01-27 15:56:22.100 4477-4477/com.shaheed.myapplication D/dalvikvm﹕ Late-enabling CheckJNI
01-27 15:56:22.420 4477-4477/com.shaheed.myapplication D/AndroidRuntime﹕ Shutting down VM
01-27 15:56:22.420 4477-4477/com.shaheed.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-27 15:56:22.430 4477-4477/com.shaheed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaheed.myapplication/com.shaheed.myapplication.SimpleHListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.shaheed.myapplication.SimpleHListActivity.onCreate(SimpleHListActivity.java:47)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
01-27 15:57:58.000 4595-4595/com.shaheed.myapplication D/AndroidRuntime﹕ Shutting down VM
01-27 15:57:58.000 4595-4595/com.shaheed.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-27 15:57:58.010 4595-4595/com.shaheed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaheed.myapplication/com.shaheed.myapplication.SimpleHListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.shaheed.myapplication.SimpleHListActivity.onCreate(SimpleHListActivity.java:46)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
提前感谢帮助。 问候。
更新 它不在演示文件中。这就是为什么我没有把它。 然后我把它放在那里仍然有错误。这是放入listView =(HListView)findViewById(R.id.hListView1)之后的logcat;在setHeader行之前。 更新了onCreate:
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
List<String> items = new ArrayList<String>();
for( int i = 0; i < 50; i++ ) {
items.add( String.valueOf( i ) );
}
mAdapter = new TestAdapter( this, R.layout.test_item_1, android.R.id.text1, items );
listView = (HListView) findViewById(R.id.hListView1);
listView.setHeaderDividersEnabled( true );
listView.setFooterDividersEnabled( true );
if( listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE_MODAL ) {
listView.setMultiChoiceModeListener( new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode( ActionMode mode, Menu menu ) {
return true;
}
@Override
public void onDestroyActionMode( ActionMode mode ) {
}
@Override
public boolean onCreateActionMode( ActionMode mode, Menu menu ) {
menu.add( 0, 0, 0, "Delete" );
return true;
}
@Override
public boolean onActionItemClicked( ActionMode mode, MenuItem item ) {
Log.d(LOG_TAG, "onActionItemClicked: " + item.getItemId());
final int itemId = item.getItemId();
if( itemId == 0 ) {
deleteSelectedItems();
}
mode.finish();
return false;
}
@Override
public void onItemCheckedStateChanged( ActionMode mode, int position, long id, boolean checked ) {
mode.setTitle( "What the fuck!" );
mode.setSubtitle( "Selected items: " + listView.getCheckedItemCount() );
}
} );
} else if( listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE ) {
listView.setOnItemClickListener(this);
}
listView.setAdapter( mAdapter );
mButton1.setOnClickListener( this );
mButton2.setOnClickListener( this );
mButton3.setOnClickListener( this );
Log.i( LOG_TAG, "choice mode: " + listView.getChoiceMode() );
}
更新了LOGCAT:
01-27 16:19:46.680 4982-4982/com.shaheed.myapplication D/AndroidRuntime﹕ Shutting down VM
01-27 16:19:46.680 4982-4982/com.shaheed.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-27 16:19:46.690 4982-4982/com.shaheed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaheed.myapplication/com.shaheed.myapplication.SimpleHListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.shaheed.myapplication.SimpleHListActivity.onCreate(SimpleHListActivity.java:44)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
内容视图错误setContentView(R.layout.activity_main);
应该是setContentView(R.layout.activity_simple_hlist);