gpt4 book ai didi

java - 按钮点击不启动 Intent ?

转载 作者:行者123 更新时间:2023-11-30 02:36:16 25 4
gpt4 key购买 nike

每当我点击一个带有设定 Intent 的按钮时,没有任何反应。我试了又试,但我无法弄清楚我的代码有什么问题。

按钮:

Button viewMedsButton = (Button)findViewById(R.id.viewMedicationButton);
viewMedsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent to open view meds
Intent medView = new Intent("android.intent.action.VIEWMEDS");
startActivity(medView);
}
});

list 中的 Intent :

<activity
android:theme="@style/MainTheme"
android:name=".viewMeds"
android:label="@string/app_name"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEWMEDS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

和 viewMeds.java:

package ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class viewMeds extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_meds);


String SDRoot = Environment.getExternalStorageDirectory().getPath();
final File output = getFileStreamPath("output.txt");
//Get the text file
File file = new File(SDRoot,"output.txt");

//Read text from file


StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;

while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.viewMedsTxt);

//Set the text
tv.setText(text);



}



public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.action_settings:
Intent prefIntent = new Intent("android.intent.action.PREFS");
startActivity(prefIntent);
break;
case R.id.exit:
finish();
break;

}

int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment() {
}
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
return rootView;
}

/* @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments.getInt(ARG_SECTION_NUMBER));
}*/
}

和 viewMeds.xml 文件:

<

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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.viewMeds">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/viewMedsTxt" />

</RelativeLayout>

非常感谢。

最佳答案

我的错误,我不小心在另一个按钮的方法中添加了我的按钮和 Intent 等,而不是 onCreate。

确保始终将您的方法放在正确的位置!

关于java - 按钮点击不启动 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com