我正在创建一个可以查看多个列表的应用,希望能够搜索列表。我是java的初学者,我确信我的代码中有一些错误,但我希望能够测试它,看看我是否可以创建应用程序。如果我发现它太难创建我将放弃该项目。我问,因为如果这是一个简单的解决方案,我很接近,那么我希望看到它。谢谢您的帮助。
主要活动
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ImageButton imagebutton1, imagebutton2, imagebutton3, imagebutton4, imagebutton5, imagebutton6;
private EditText SearchBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebutton1 = (ImageButton)findViewById(R.id.metal_button);
imagebutton2 = (ImageButton) findViewById(R.id.imageButton2);
imagebutton3 = (ImageButton) findViewById(R.id.imageButton3);
imagebutton4 = (ImageButton) findViewById(R.id.imageButton4);
imagebutton5 = (ImageButton) findViewById(R.id.imageButton5);
imagebutton6 = (ImageButton) findViewById(R.id.imageButton6);
SearchBox = (EditText) findViewById(R.id.search_box);
String[] MetalList= {"metal", "metal cans", "aluminum foil", "copper", "steel", "scrap metal",
"hangers", "aluminum can", "knife", "exercise equipment"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
MetalList);
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String MetalPicked = "Please put in Green Bin";
Toast.makeText(MainActivity.this, MetalPicked, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onMetalButtonClick(View view) {
Intent getmetalscreenIntent= new Intent(this,
MetalScreen.class);
final int result = 1;
getmetalscreenIntent.putExtra("callingActivity", "Main Activity");
getmetalscreenIntent.putExtra("MetalActivity" , "MetalList");
startActivityForResult(getmetalscreenIntent, result);
}
}
清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".MetalScreen"
android:label="MetalScreen"
android:theme="@style/AppTheme"/>
</application>
金属屏幕
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class MetalScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metal_layout);
Intent activityThatCalled = getIntent();
String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
String previousActivity2 = activityThatCalled.getExtras().getString("MetalActivity");
ListView callingActivityList = (ListView) findViewById(R.id.metal_list_view);
}
}
Activity_Main
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rcswwheredoirecyclethisproject.MainActivity"
android:gravity="top"
android:background="@drawable/recyclesymbol">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/LinearLayout2">
<ImageButton
android:id="@+id/metal_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton1text"
android:onClick="onMetalButtonClick"
android:background="@drawable/metal_texture"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:cropToPadding="true"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton2text"
android:background="@drawable/biohazard"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton3text"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton4text"/>
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton5text"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton6text"
android:clickable="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5"
android:id="@+id/LinearLayout2"
android:layout_below="@+id/LinearLayout3">
<EditText
android:layout_width="200dp"
android:layout_height="150dp"
android:inputType="text"
android:hint="@string/EditText"
android:id="@+id/search_box"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/LinearLayout3"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RCSW Recycle"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Do I Recycle This?"
android:textSize="25sp"/>
</LinearLayout>
<requestFocus />
</RelativeLayout>
金属布局
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left"
android:id="@+id/metal_list_view"
android:padding="10dp">
</ListView>
</LinearLayout>
钉刺
<resources>
<string name="app_name">RCSW Where Do I Recycle</string>
<string name="content">Image</string>
<string name="menu_settings">Settings</string>
<string name="ImageButton1text">Metal</string>
<string name="ImageButton2text">Hazard</string>
<string name="ImageButton3text">Paper</string>
<string name="ImageButton4text">Glass</string>
<string name="ImageButton5text">Plastic</string>
<string name="ImageButton6text">Other</string>
<string name="EditText">Search Box</string>
<string name="action_settings">RCSWRecycle</string>
</resources>
答案 0 :(得分:0)
您的ActivityMain xml布局文件中没有任何Listview,但是,您的onCreate方法正在调用它并设置适配器!您可能在theListView上获得了一个nullpointer?
你打电话两次:
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
返回null,因为android无法在activity_main.xml上找到该视图。
编辑: 启动主活动时,您只能使用activity_layout上的元素。由于ListView在金属版面上,而不在activity_layout上,因此无法对其进行引用。
您需要做的是找到关于您的金属活动的视图。像这样。 您的创建MainActivity的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebutton1 = (ImageButton)findViewById(R.id.metal_button);
imagebutton2 = (ImageButton) findViewById(R.id.imageButton2);
imagebutton3 = (ImageButton) findViewById(R.id.imageButton3);
imagebutton4 = (ImageButton) findViewById(R.id.imageButton4);
imagebutton5 = (ImageButton) findViewById(R.id.imageButton5);
imagebutton6 = (ImageButton) findViewById(R.id.imageButton6);
SearchBox = (EditText) findViewById(R.id.search_box);
}
然后在你的Metal Activity上你应该有这样的东西:(在这个布局中,你找到listview,创建和设置适配器)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metal_layout);
Intent activityThatCalled = getIntent();
String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
String previousActivity2 = activityThatCalled.getExtras().getString("MetalActivity");
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
MetalList);
String[] MetalList= {"metal", "metal cans", "aluminum foil", "copper", "steel", "scrap metal",
"hangers", "aluminum can", "knife", "exercise equipment"};
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String MetalPicked = "Please put in Green Bin";
Toast.makeText(MainActivity.this, MetalPicked, Toast.LENGTH_LONG).show();
}
});
}
答案 1 :(得分:0)
Rui Santos是对的。您只能访问活动setContentView()
方法中定义的文件中的xml元素。在这种情况下,在您的主要活动中,您声明setContentView(R.layout.activity_main)
。
稍后您致电findViewById(R.id.metal_list_view)
,但身份metal_list_view
位于metal_layout
。 findViewById
次搜索activity_main
,找不到metal_list_view
,然后返回null
。当您尝试将该null转换为ListView
时,它将抛出NullPointerException
。
如果你收到NPE以外的其他错误,你应该告诉我们,我们会帮助解决这个问题,但这个问题需要解决。
你应该可以通过移动
让它运行String[] MetalList= {"metal", "metal cans", "aluminum foil", "copper", "steel", "scrap metal",
"hangers", "aluminum can", "knife", "exercise equipment"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
MetalList);
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String MetalPicked = "Please put in Green Bin";
Toast.makeText(MetalActivity.this, MetalPicked, Toast.LENGTH_LONG).show(); // edited to remove outdated reference to MainActivity
}
});
从您的主要活动onCreate()
到您的金属活动onCreate()
有点不幸的是,Android中的活动和片段与其xml布局紧密耦合。每个活动或片段仅包含对单个布局的引用,但包含框架元素的布局可以包含其他布局。在这种情况下,您的金属活动会与您的布局相关联,该布局包含&#39; listview&#39;您正在尝试修改,因此需要在金属活动中进行修改。我并没有真正弄清楚你的意图是什么,但你可能想要的是一个带有两个片段的单一活动。然后每个片段都有一个布局,活动包含对两个片段的引用,并且通过使用一些回调接口,您可以在三者之间进行通信,并托管您活动中的所有逻辑。
仔细查看您正在做的事情,您可能需要在主活动布局中添加framelayout
,将您的金属活动转换为片段,然后将片段附加到framelayout
。这将为您提供一种活动内活动感,可以提高可重用性。如果不知道你的目的是什么,很难说。